循环队列(顺序)二

其实感觉这玩意真的好麻烦。。但是自己实现一遍还是很有踏实感的。。

SeQueue:

  1. /*============================================================================= 
  2. # 
  3. #      Author: liangshu - cbam  
  4. # 
  5. #      QQ : 756029571  
  6. # 
  7. #      School : 哈尔滨理工大学  
  8. # 
  9. #      Last modified: 2015-10-26 14:27 
  10. # 
  11. #     Filename: 循环队列(顺序)二 - 副本 (3).cpp 
  12. # 
  13. #     Description:  
  14. #        The people who are crazy enough to think they can change the world, are the ones who do !  
  15. =============================================================================*/  
  16. #  
  17. using namespace std;  
  18. typedef int Queue_entry;  
  19. const int maxqueue = 6;  
  20. enum Error_code{overflow, underflow,success};  
  21. class Queue{  
  22.   public:  
  23.       Queue();  
  24.       bool Empty()const;  
  25.       void Print_queue()const;  
  26.       Error_code serve();  
  27.       Error_code append(const Queue_entry &item);  
  28.       Error_code retrieve(Queue_entry &item)const;  
  29.   protected:  
  30.     int num;  
  31.     int front_, rear_;  
  32.     Queue_entry Entry[maxqueue];  
  33. };  
  34.   
  35. class Extended_queue: public Queue{  
  36. public:  
  37.     Extended_queue(const Queue &tp):Queue(tp){}  
  38.     bool Full()const;  
  39.     int Size()const;  
  40.     void Clear();  
  41.     Error_code serve_and_retrieve(Queue_entry &item);  
  42. };  
  43. Queue::Queue(){  
  44.   num = 0;  
  45.   rear_ = maxqueue - 1;  
  46.   front_ = 0;  
  47. }  
  48.   
  49. bool Queue::Empty()const{  
  50.    return num == 0;  
  51. }  
  52.   
  53. Error_code Queue::append(const Queue_entry &item){  
  54.    if(num >= maxqueue){  
  55.     return overflow;  
  56.    }  
  57.    num++;  
  58.    rear_ = (rear_ + 1) == maxqueue ? 0 : (rear_ + 1);  
  59.    Entry[rear_] = item;  
  60.    return success;  
  61. }  
  62.   
  63. Error_code Queue::serve(){  
  64.    if(num <= 0){  
  65.     return underflow;  
  66.    }  
  67.    num--;  
  68.    front_ = ((front_ + 1) == maxqueue ? 0 : front_ + 1);  
  69.    return success;  
  70. }  
  71. void Queue::Print_queue()const{  
  72.   cout<<"front -> ";  
  73.   for(int i = 0; i < num; i++){  
  74.     cout<<Entry[(front_ + i) % maxqueue]<<" -> ";  
  75.   }  
  76.   cout<<"rear"<<endl;  
  77. }  
  78. Error_code Queue::retrieve(Queue_entry &item)const{  
  79.   if(num <= 0){  
  80.     return underflow;  
  81.   }  
  82.   item = Entry[front_];  
  83.   return success;  
  84. }  
  85.   
  86. bool Extended_queue::Full()const{  
  87.     if(num == maxqueue){  
  88.         return true;  
  89.     }  
  90.     return false;  
  91. }  
  92.   
  93. int Extended_queue::Size()const{  
  94.   return num;  
  95. }  
  96. void Extended_queue::Clear(){  
  97.   this -> num = 0;  
  98.   this -> front_ = 0;  
  99.   this -> rear_ = maxqueue - 1;  
  100. }  
  101. Error_code Extended_queue::serve_and_retrieve(Queue_entry &item){  
  102.   if(Empty()){  
  103.     return underflow;  
  104.   }  
  105.   item = Entry[front_];  
  106.   this -> serve();  
  107.   return success;  
  108. }  

Test.cpp:

  1. #include<iostream>  
  2. #include"SeQueue.h"  
  3. int main()  
  4. {  
  5.     Queue queue_;  
  6.     for(int i = 1; i <= maxqueue; i++){  
  7.       queue_.append(i);  
  8.     }  
  9.     queue_.Print_queue();  
  10.     int x;queue_.retrieve(x);  
  11.     cout<<"t ="<<x<<endl;  
  12.     queue_.serve();  
  13.     queue_.retrieve(x);  
  14.     cout<<x<<endl;  
  15.     queue_.Print_queue();  
  16.     queue_.append(7);  
  17.     queue_.append(8);  
  18.     queue_.Print_queue();  
  19.     cout<<"继承:\n"<<endl;  
  20.   
  21.     Extended_queue queue_2(queue_);  
  22.     queue_2.Print_queue();  
  23.     queue_2.retrieve(x);  
  24.     cout<<x<<endl;  
  25.     queue_2.serve_and_retrieve(x);  
  26.     queue_2.Print_queue();  
  27.     queue_2.Clear();  
  28.     queue_2.Print_queue();  
  29.     cout<<queue_2.retrieve(x)<<endl;  
  30.     return 0; 
  31. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值