- #include"stdio.h"
- #include"stdlib.h"
- typedefintElementType;
- typedefstructQueue
- {
- intrear,front;
- ElementType*elements;
- intMaxSize;
- }Queue;
- voidInitQueue(Queue*Q,intsz)//初始化
- {
- Q->MaxSize=sz;
- Q->elements=(ElementType*)malloc(sizeof(ElementType)*Q->MaxSize);
- Q->front=Q->rear=0;
- }
- voidfreeQueue(Queue*Q,intsz)//释放空间
- {
- free(Q->elements);
- }
- voidMakeEmpty(Queue*Q)//置空
- {
- Q->front=Q->rear=0;
- }
- intLength(Queue*Q)//返回长度
- {
- return(Q->rear-Q->front+Q->MaxSize)%(Q->MaxSize);
- }
- intIsFull(Queue*Q)//判断是否为满
- {
- if(Q->rear!=0&&(Q->front==(Q->rear)%(Q->MaxSize)))
- return1;
- else
- return0;
- }
- intIsEmpty(Queue*Q)//判断是否为空
- {
- if(Q->front==Q->rear)
- return1;
- else
- return0;
- }
- voidEnQueue(Queue*Q)//进队
- {ElementTypeitem;
- scanf("%d",&item);
- while((!IsFull(Q))&&(item!=-1))
- {
- Q->elements[Q->rear]=item;
- Q->rear=(Q->rear+1)%(Q->MaxSize);
- scanf("%d",&item);
- }
- }
- ElementTypeDeQueue(Queue*Q)
- {
- ElementTypeitem;
- if(!IsEmpty(Q))
- {
- item=Q->elements[Q->front];
- Q->front=(Q->front+1)%(Q->MaxSize);
- returnitem;
- }
- else
- {
- printf("对空!\n");
- exit(1);
- }
- }
- ElementTypeGetFront(Queue*Q)
- {
- if(!IsEmpty(Q))
- returnQ->elements[Q->front];
- else
- {
- printf("队空!\n");
- exit(1);
- }
- }
- voidmain()
- {
- inti;
- QueueQ;
- InitQueue(&Q,10);
- EnQueue(&Q);
- for(i=0;i<10;i++)
- if(!IsEmpty(&Q))
- printf("%-5d",DeQueue(&Q));
- }
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/817866