#include <stdio.h> #include <stdlib.h> #define QueueSize 10 typedef struct { char data [QueueSize]; int front,rear; }SqQueue; void InitQueue(SqQueue &qu) //初始化队列 { qu.rear=qu.front=0; } int EnQueue(SqQueue &qu, char x) //进队 { if((qu.rear+1)%QueueSize==qu.front) return 0; else { qu.rear=(qu.rear+1)%QueueSize; qu.data[qu.rear]=x; return 1; } } int DeQueue(SqQueue &qu,char &x) //出队 { if(qu.rear==qu.front) return 0; else { qu.front=(qu.front+1)%QueueSize; x=qu.data[qu.front]; return 1; } } int GetHead(SqQueue qu,char x) //取队头元素 { if(qu.rear=qu.front) return 0; else { x=qu.data[(qu.front+1)%QueueSize]; return 1; } } int QueuEmpty(SqQueue qu) //判断队空 { if(qu.front==qu.rear) return 1; else return 0; } int main() { SqQueue qu; char e; InitQueue(qu); printf("Queue is %s ",(QueuEmpty(qu)==1?"Empty":"Not Empty")); printf("a in Queue ");EnQueue(qu,'a'); printf("b in Queue ");EnQueue(qu,'b'); printf("c in Queue ");EnQueue(qu,'c'); printf("d in Queue ");EnQueue(qu,'d'); printf("Queue is %s ",(QueuEmpty(qu)==1?"Empty":"Not Empty")); GetHead(qu,e); printf("The 1st Character in Queue: %c ",e); printf("Order of this Queue: "); while(!QueuEmpty(qu)) { DeQueue(qu,e); printf("%c ",e); } printf(" "); return 0; }