数组实现的 环形队列

 

C++数据结构环形队列Deque实现

标签: c++数据结构队列
  962人阅读  评论(0)  收藏  举报
  分类:

队列是一种常见的数据结构,生活中的排队买票,排队等车,都是队列。队列的特点是先进先出FIFO。队列可以基于数组实现,也可以基于链表实现。

这里是基于链表实现的。每次出队操作,头指针后移,每次入队,尾指针也后移。因为数组是固定长度连续空间,首位指针后移,队尾可插入区域会越来越小。当然,可以每次出队,整个队列前移,但是数组移动需要牺牲性能。环形队列可以解决数组移动的缺点,当尾指针超出数组末尾时,尾指针移动数组头部。这就将数组虚拟成了一个环形,只要

队列长度没达到最大值,就可以插入,而不用移动数组。

下面是头文件

[cpp]  view plain  copy
  1. #pragma once  
  2. class RingQueue  
  3. {  
  4. public:  
  5.     RingQueue(int maxSize);  
  6.     ~RingQueue();  
  7.     void clearQueue();  
  8.     int length() const;  
  9.     void traverse();  
  10.     bool enQueue(int item);  
  11.     bool deQueue(int& item);  
  12.     bool isEmpty() const;  
  13.     bool isFull() const;  
  14. private:  
  15.     int *m_pQueue;  //数组指针  
  16.     int m_length;   //长度  
  17.     int m_capacity; //最大长度  
  18.     int m_iHead;    //队头  
  19.     int m_iTail;    //队尾  
  20.     int move(int& head_or_tail);  
  21. };  

头文件里定义了队列的基本操作。注意 int move(int& head_or_tail) 函数的作用是安全的移动 head 和tail 指向的位置。超出界线重新判断。

下面是代码实现

[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include "RingQueue.h"  
  3. using namespace std;  
  4.   
  5. RingQueue::RingQueue(int maxSize)  
  6. {  
  7.     m_capacity = maxSize;  
  8.     m_iHead = 0;  
  9.     m_iTail = 0;  
  10.     m_length = 0;  
  11.     m_pQueue = new int[maxSize];  
  12. }  
  13.   
  14. RingQueue::~RingQueue()  
  15. {  
  16.     delete[] m_pQueue;  
  17. }  
  18.   
  19. void RingQueue::clearQueue()  
  20. {  
  21.     m_iHead = 0;  
  22.     m_iTail = 0;  
  23.     m_length = 0;  
  24. }  
  25.   
  26. int RingQueue::length() const  
  27. {  
  28.     return m_length;  
  29. }  
  30.   
  31. void RingQueue::traverse()  
  32. {  
  33.     if (isEmpty()) return;  
  34.     int f = m_iHead, t = m_iTail;  
  35.     do  
  36.     {  
  37.         cout<<m_pQueue[f]<<" ";  
  38.     } while (move(f)!=t);  
  39.     cout << endl;  
  40. }  
  41.   
  42. bool RingQueue::enQueue(int item)  
  43. {  
  44.     if (isFull()) return false;  
  45.     m_pQueue[m_iTail] = item;  
  46.     move(m_iTail);  
  47.     m_length++;  
  48. }  
  49.   
  50. bool RingQueue::deQueue(int& item)  
  51. {  
  52.     if (isEmpty()) return false;  
  53.     item = m_pQueue[m_iHead];  
  54.     move(m_iHead);  
  55.     m_length--;  
  56. }  
  57.   
  58. bool RingQueue::isEmpty() const  
  59. {  
  60.     return m_length==0;  
  61. }  
  62.   
  63. bool RingQueue::isFull() const  
  64. {  
  65.     return m_length == m_capacity;  
  66. }  
  67.   
  68. int RingQueue::move(int& head_or_tail)  
  69. {  
  70.     ++head_or_tail;  
  71.     head_or_tail%=m_capacity;  
  72.     return head_or_tail;  
  73. }  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值