循环队列实验

  1. #include <iostream>  
  2. using namespace std;   
  3. const int Queue_Size=100;  
  4. typedef struct circlQueue  
  5. {  
  6.        char *elem;  
  7.        int rear;  
  8.        int front;  
  9.        int queueSize;  
  10. }circlQueue;  
  11.    
  12.    
  13.    
  14.  
  15. void initQueue_C(circlQueue &Q)  
  16. {  
  17.        Q.elem=new char[Queue_Size];  
  18.        Q.front=Q.rear=0;
  19.        Q.queueSize=Queue_Size;  
  20. }  
  21.    

  22. void destroyQueue_C(circlQueue &Q)  
  23. {  
  24.        delete []Q.elem;  
  25.        Q.front=Q.rear=0;  
  26.        Q.queueSize=0;  
  27. }  
  28.    
  29. int  lengthQueue_C(circlQueue &Q)  
  30. {  
  31.        int length;  
  32.        length=(Q.rear-Q.front+Q.queueSize)%Q.queueSize; 
  33.        return length;  
  34. }  
  35.    
  36.    
  37. void enterQueue_C(circlQueue &Q,char x)  
  38. {  
  39.        if(((Q.rear+1)%Q.queueSize)==Q.front) 
  40.               cout<<"Queue OverFlow!";  
  41.        Q.elem[Q.rear]=x;  
  42.        Q.rear=(Q.rear+1)%Queue_Size; 
  43. }  
  44.    

  45. char outputQueue_C(circlQueue &Q)  
  46. {  
  47.        char e;  
  48.        if(Q.rear==Q.front)  
  49.               cout<<"Queue Empty";  
  50.        e=Q.elem[Q.front];  
  51.        Q.front=(Q.front+1)%Q.queueSize;;  
  52.        return e;  
  53. }  
  54.    
  55. void main()  
  56. {  
  57.        circlQueue Q;  
  58.        initQueue_C(Q);  
  59.        enterQueue_C(Q,'a');  
  60.        enterQueue_C(Q,'b');  
  61.        enterQueue_C(Q,'c');  
  62.        cout<<outputQueue_C(Q)<<endl;  
  63.        cout<<outputQueue_C(Q)<<endl;  
  64.        destroyQueue_C(Q);  
  65. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值