#include<iostream> using namespace std; #define MaxSize 50 typedef char ElemType; typedef struct { ElemType data[MaxSize]; int front ,rear; }SqQueue; void InitQueue(SqQueue *&q) { q=(SqQueue *)malloc (sizeof(SqQueue)); q->front=q->rear=0; } void ClearQueue(SqQueue *&q) { free(q); } int QueueEmpty(SqQueue *&q) { return (q->rear==q->front); } int enQueue(SqQueue * &q,ElemType e) { if(q->rear+1%MaxSize==q->front) //隊滿 return 0; q->rear=(q->rear+1)%MaxSize; q->data[q->rear]=e; return 1; } int deQueue(SqQueue *&q,ElemType &e) { if(q->rear==q->front) //隊空 return 0; q->front=(q->front+1)%MaxSize; e=q->data[q->front]; return 1; } void main() { ElemType e='0'; SqQueue *q; cout<<"初始化隊列;"<<endl; InitQueue(q); cout<<"判斷隊列是否為空:"; if(QueueEmpty(q)) cout<<"空;"<<endl; else cout<<"非空;"<<endl; cout<<"將元素a,b,c進隊:"<<endl; enQueue(q,'a'); enQueue(q,'b'); enQueue(q,'c'); deQueue(q,e); cout<<"出隊元素為:"<<e<<endl; ClearQueue(q); }