循环队列

  1.  #include<iostream>
  2. #include<malloc.h>
  3. #include<stdlib.h>
  4. using namespace std; 
  5. #define OK 1
  6. #define ERROR 0
  7. #define OVERFLOW -2
  8. #define MAXSIZE 10
  9. typedef int QElemType; 
  10. typedef int Status;
  11. typedef struct {
  12.         QElemType *base;
  13.         int front;
  14.         int rear;
  15.         }SqQueue;
  16.         
  17. Status InitQueue(SqQueue &Q)
  18. {
  19.       Q.base=(QElemType *)malloc(MAXSIZE*sizeof(QElemType));
  20.       if(!Q.base) exit(OVERFLOW);
  21.       Q.front=Q.rear=0;
  22.       return OK; }
  23. Status EnQueue(SqQueue &Q,QElemType e)
  24. {
  25.       if((Q.rear+1)%MAXSIZE==Q.front) return ERROR;
  26.       Q.base[Q.rear]=e;
  27.       Q.rear=(Q.rear+1)%MAXSIZE;
  28.       return OK; }
  29. Status DeQueue(SqQueue &Q,QElemType &e)
  30. {
  31.        if(Q.front==Q.rear) return ERROR;
  32.        e=Q.base[Q.front];
  33.        Q.front=(Q.front+1)%MAXSIZE;
  34.        return OK;}
  35. Status PrintQueue(SqQueue Q)
  36. {
  37.    for(;Q.front!=Q.rear;Q.front=(Q.front+1)%MAXSIZE)
  38.    cout<<Q.base[Q.front]<<" ";
  39.    cout<<endl;
  40.    return OK;   }
  41.    
  42. int main()
  43. {
  44.  SqQueue Q;
  45.  QElemType e;
  46.  InitQueue(Q);
  47.  while(cin>>e)
  48.   if(!EnQueue(Q,e)) break;
  49.  PrintQueue(Q);
  50.  if(DeQueue(Q,e)){
  51.   cout<<e<<endl;
  52.   PrintQueue(Q);}
  53.  else cout<<"The queue is empty!"<<endl;
  54.  return 0;
  55. }
  56. 5 3 2 6 1
  57. ^Z
  58. 5 3 2 6 1
  59. 5
  60. 3 2 6 1
  61. 请按任意键继续. . .
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值