顺序循环队列-C++模板类实现

顺序循环队列-C++模板类实现

  1 /*顺序循环队列模板类
  2  * (rear + 1)%QueSize == front时,队列满(存储了QueSize-1个元素)
  3  * rear == front时,队列空
  4  *入队:rear = (rear+1)%QueSize;
  5  *出队:front = (front+1)%QueSize;
  6  *当前队列存储元素的个数:(rear - front + QueSize)%QueSize
  7  */
  8 #include <iostream>
  9 using namespace std;
 10 #include <sstream>
 11 #include <limits>
 12 
 13 #define QueSize 10
 14 template <typename T>
 15 class SeQueue{
 16         T data[QueSize];
 17         int rear;
 18         int front;
 19         public:
 20                 SeQueue(int front = 0, int rear = 0): front(front), rear(rear){}
 21                 void InitQueue(){
 22                         T e;
 23                         while (cin >> e)
 24                         {
 25                                 data[rear] = e;
 26                                 rear = (rear+1)%QueSize;
 27                                 if ((rear+1)%QueSize == front)
 28                                 {
 29                                         cerr<< "Full!!" << endl;
 30                                         break;
 31                                 }
 32 
 33                         }
 34                 }
 35                 bool QueueEmpty(){
 36                         if (rear == front)
 37                                 return true;
 38                         else
 39                                 return false;
 40                 }
 41                 void EnQueue (){
 42                         T e;
 43                         cin.clear();//清除错误状态
 44                         cin.ignore(std::numeric_limits<std::streamsize>::max());//清理输入流中所有的数据
 45                         /*while (!(cin >> e))
 46                         {
 47                                 cin.ignore();//丢弃输入流中的字符
 48                         }*/
 49                         while (cin >> e)
 50                         {
 51                                 if ((rear+1)%QueSize == front)
 52                                 {
 53                                         cout << "Full!!" << endl;
 54 
 55                                 }
 56                                 else
 57                                 {
 58                                         data[rear] = e;
 59                                         rear = (rear+1)%QueSize;
 60                                 }
 61                         }
 62 
 63                 }
 64                 void DeQueue (T& e){
 65                         if (rear == front)
 66                         {
 67                                 cout << "Empty!!" << endl;
 68                         }
 69                         else
 70                         {
 71                                 e = data[front];
 72                                 front = (front+1)%QueSize;
 73                         }
 74                 }
 75                 void QueueLength (int& Size){
 76                         Size = (rear - front + QueSize)%QueSize;
 77                 }
 78                 void show (){
 79                         if (rear == front)
 80                         {
 81                                 cerr << "Empty!!" << endl;
 82                         }
 83                         else
 84                         {
 85                                 int p = front;//定义打印下标
 86                                 for (int i = 0; i < (rear - front + QueSize)%QueSize; i++)
 87                                 {
 88                                         cout << data[p] << ' ';
 89                                         p = (p+1)%QueSize;
 90                                 }
 91                         }
 92                         cout << endl;
 93                 }
 94 };
 95 int main()
 96 {
 97         SeQueue<int> Q;
 98         Q.InitQueue();
 99         cout << "------show-------" << endl;
100         Q.show();
101         cout << "------EnQue------" << endl;
102         Q.EnQueue();
103         cout << endl;
104         int Size;
105         Q.QueueLength(Size);
106         cout << Size << endl;
107         Q.show();
108         cout << "------DeQueue---" << endl;
109         int e;
110         Q.DeQueue(e);
111         cout << e << endl;
112         Q.show();
113         cout << "-----end-------" << endl;
114         return 0;
115 }
116 
117 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值