鱼C论坛

 找回密码
 立即注册
查看: 1997|回复: 0

[学习笔记] 循环顺序队列

[复制链接]
发表于 2018-4-20 21:16:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
《数据结构C++描述》清华大学出版社    任燕编著

小弟还是老样子重载了输出运算符,并且写了一个main函数使程序可以运行,
加油!
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstdbool>
  4. #include <assert.h>
  5. using namespace std;

  6. template <typename elemtype>
  7. class SqQueue{
  8. public:
  9.         //循环队列置空
  10.         void clear();

  11.         //出队列
  12.         bool deQueue(elemtype &e);

  13.         //进队列
  14.         bool enQueue(elemtype e);

  15.         //读取循环队列队头元素
  16.         bool getFront(elemtype &e);

  17.         //求循环队列的元素的个数
  18.         int getLength();

  19.         //判断队列是否为空
  20.         bool isEmpty();

  21.         //判断队列是否已满
  22.         bool isFull();

  23.         //重载赋值运算符
  24.         SqQueue operator=(SqQueue right);

  25.         //构造函数
  26.         SqQueue(int size = 10);

  27.         //析构函数
  28.         virtual ~SqQueue();

  29.         //拷贝构造函数
  30.         SqQueue(SqQueue &other);

  31.         //重载输出运算符
  32.         template <typename out_put>
  33.         friend ostream& operator <<(ostream& out, SqQueue<out_put> other);

  34. protected:
  35.         int rear;//队尾指针
  36.         int front;//对头指针
  37.         int queueSize;//循环队列最大存储空间
  38.         elemtype *base;//队列动态存储空间的首地址
  39. };


  40. template <typename elemtype>
  41. void SqQueue<elemtype>::clear(){
  42.         front = rear;
  43. }

  44. //出队列
  45. template <typename elemtype>
  46. bool SqQueue<elemtype>::deQueue(elemtype &e){
  47.         if (isEmpty()){
  48.                 return false;
  49.         }
  50.         e = base[front];
  51.         front = (front + 1) % queueSize;//这里要取模,形成循环队列
  52.         return true;
  53. }

  54. //进队列
  55. template <typename elemtype>
  56. bool SqQueue<elemtype>::enQueue(elemtype e){
  57.         if (isFull()){
  58.                 return false;
  59.         }
  60.         base[rear] = e;
  61.         rear = (rear + 1) % queueSize;
  62.         return true;
  63. }

  64. //读取队头元素
  65. template <typename elemtype>
  66. bool SqQueue<elemtype>::getFront(elemtype &e){
  67.         if (isEmpty()){
  68.                 return false;
  69.         }
  70.         e = base[front];
  71.         return true;
  72. }

  73. //求队列中元素的个数
  74. template <typename elemtype>
  75. int SqQueue<elemtype>::getLength(){
  76.         return (rear - front + queueSize) % queueSize;
  77. }

  78. //判断是否为空
  79. template <typename elemtype>
  80. bool SqQueue<elemtype>::isEmpty(){
  81.         return front == rear ? true : false;
  82. }

  83. //判断队列是否已满
  84. template <typename elemtype>
  85. bool SqQueue<elemtype>::isFull(){
  86.         return (rear + 1) % queueSize == front ? true : false;
  87. }

  88. //重载赋值运算符
  89. template <typename elemtype>
  90. SqQueue<elemtype> SqQueue<elemtype>::operator=(SqQueue<elemtype> right){
  91.         if (this != &right){
  92.                 if (queueSize != right.queueSize){
  93.                         delete[] base;
  94.                         base = new elemtype[right.queueSize];
  95.                         assert(base != 0);
  96.                         queueSize = right.queueSize;
  97.                 }

  98.                 front = right.front;
  99.                 rear = right.rear;

  100.                 for (int i = front; i%queueSize != rear;){
  101.                         base[i] = right.base[i];
  102.                         i = (i + 1) % queueSize;
  103.                 }
  104.         }
  105.         return *this;
  106. }

  107. template <typename elemtype>
  108. SqQueue<elemtype>::SqQueue(int size = 10){
  109.         base = new elemtype[size];
  110.         assert(base != 0);
  111.         front = rear = 0;
  112.         queueSize = size;
  113. }

  114. template <typename elemtype>
  115. SqQueue<elemtype>:: ~SqQueue(){
  116.         delete[]base;
  117. }

  118. //拷贝初始化构造函数
  119. template <typename elemtype>
  120. SqQueue<elemtype>::SqQueue(SqQueue &other){
  121.         base = new elemtype[other.queueSize];
  122.         assert(base != 0);
  123.         queueSize = other.queueSize;

  124.         front = other.front;
  125.         rear = other.rear;

  126.         for (int i = front; i%queueSize != rear;){
  127.                 base[i] = other.base[i];
  128.                 i = (i + 1) % queueSize;
  129.         }
  130. }

  131. //重载输出运算符
  132. template <typename out_put>
  133. ostream& operator<<(ostream& out, SqQueue<out_put> other){
  134.         for (int i = other.front; i%other.queueSize != other.rear;){
  135.                 out << other.base[i] << "\t";
  136.                 i = (i + 1) % other.queueSize;
  137.         }
  138.         return out;
  139. }


  140. int main(void){
  141.         SqQueue<int> sq1;//默认传入的参数是10
  142.         for (int i = 1; i <= 10; i++){
  143.                 sq1.enQueue(i);
  144.         }

  145.         cout << "现在队列为:" << endl;
  146.         cout << sq1 << endl;

  147.         cout << "现在测试出队列函数:" << endl;
  148.         cout << "只出前五个:" << endl;
  149.         for (int i = 0; i < 5; i++){
  150.                 int e;
  151.                 sq1.deQueue(e);
  152.                 cout << "出队列:" << e << endl;
  153.         }
  154.         cout << "现在队列为:" << endl;
  155.         cout << sq1 << endl;
  156.         return 0;
  157. }
复制代码


运行结果:
现在队列为:
1       2       3       4       5       6       7       8       9
现在测试出队列函数:
只出前五个:
出队列:1
出队列:2
出队列:3
出队列:4
出队列:5
现在队列为:
6       7       8       9
请按任意键继续. . .
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 22:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表