顺序循环队列基本操作(三)
/*增设表示元素个数的数据*/ #include<stdio.h> #define MaxSize 10 typedef char ElemType; typedef struct { ElemType data[MaxSize]; int front,rear; int size; }SqQueue; bool InitQueue(SqQueue &q) { q.front=q.rear=0; //初始化队头队尾指针 q.size=0; return true; } bool EmptyQueue(SqQueue q) { if(q.size==0) //队空的条件:size=0 return true; return false; } bool EnQueue(SqQueue &q,ElemType e) { if(q.size==MaxSize) //队满的条件:size==MaxSize return false; q.data[q.rear++]=e; return true; } bool OutQueue(SqQueue &q,ElemType &e) { if(q.rear==q.front) //判空 return false; e=q.data[q.front++]; return true; } bool HeadQueue(SqQueue q,ElemType &e) { if(q.rear==q.front) return false; e=q.data[q.front]; return true; } void main() { SqQueue q; InitQueue(q); ElemType e; printf("The Queue is %s\n",(EmptyQueue(q)?"Empty!":"UnEmpty!")); EnQueue(q,'a'); EnQueue(q,'b'); EnQueue(q,'c'); HeadQueue(q,e); printf("Queue_Head=%c\n",e); OutQueue(q,e); HeadQueue(q,e); printf("Queue_Head=%c\n",e); }