- #include<iostream>
- #include<malloc.h>
- #include<stdlib.h>
- using namespace std;
- #define OK 1
- #define ERROR 0
- #define OVERFLOW -2
- #define MAXSIZE 10
- typedef int QElemType;
- typedef int Status;
- typedef struct {
- QElemType *base;
- int front;
- int rear;
- }SqQueue;
- Status InitQueue(SqQueue &Q)
- {
- Q.base=(QElemType *)malloc(MAXSIZE*sizeof(QElemType));
- if(!Q.base) exit(OVERFLOW);
- Q.front=Q.rear=0;
- return OK; }
- Status EnQueue(SqQueue &Q,QElemType e)
- {
- if((Q.rear+1)%MAXSIZE==Q.front) return ERROR;
- Q.base[Q.rear]=e;
- Q.rear=(Q.rear+1)%MAXSIZE;
- return OK; }
- Status DeQueue(SqQueue &Q,QElemType &e)
- {
- if(Q.front==Q.rear) return ERROR;
- e=Q.base[Q.front];
- Q.front=(Q.front+1)%MAXSIZE;
- return OK;}
- Status PrintQueue(SqQueue Q)
- {
- for(;Q.front!=Q.rear;Q.front=(Q.front+1)%MAXSIZE)
- cout<<Q.base[Q.front]<<" ";
- cout<<endl;
- return OK; }
- int main()
- {
- SqQueue Q;
- QElemType e;
- InitQueue(Q);
- while(cin>>e)
- if(!EnQueue(Q,e)) break;
- PrintQueue(Q);
- if(DeQueue(Q,e)){
- cout<<e<<endl;
- PrintQueue(Q);}
- else cout<<"The queue is empty!"<<endl;
- return 0;
- }
- 5 3 2 6 1
- ^Z
- 5 3 2 6 1
- 5
- 3 2 6 1
- 请按任意键继续. . .
循环队列
最新推荐文章于 2023-08-09 22:35:27 发布