• 支持模板的C++循环队列设计实现与优化


    1. 原理

        队列是带先入先出FIFO特性的数据结构。为了充分利用队列,顺序队列常常做成一个逻辑上的循环队列。

      注意:空队时rear等于front,满队时front = (rear+1) mod N,必须空一个位置。

    2. 设计

    2.1 属性

    1. m_size 队列大小
    2. m_front, 队头
    3. m_rear, 队尾
    4. m_data, 数据存存储区

    2.2 方法

    1. isEmpty()
    2. isFull()
    3. push()
    4. pop()

    3. 原始实现

    3.1 类实现

    1. #include
    2. using namespace std;
    3. template <class T>
    4. class CycleQueue
    5. {
    6. private:
    7. unsigned int m_size;
    8. int m_front;
    9. int m_rear;
    10. T* m_data;
    11. public:
    12. CycleQueue(unsigned size)
    13. :m_size(size),
    14. m_front(0),
    15. m_rear(0)
    16. {
    17. m_data = new T[size];
    18. }
    19. ~CycleQueue()
    20. {
    21. delete [] m_data;
    22. }
    23. bool isEmpty()
    24. {
    25. return m_front == m_rear;
    26. }
    27. bool isFull()
    28. {
    29. return m_front == (m_rear + 1)%m_size;
    30. }
    31. void push(T ele) throw(bad_exception)
    32. {
    33. if(isFull())
    34. {
    35. throw bad_exception();
    36. }
    37. m_data[m_rear] = ele;
    38. m_rear = (m_rear + 1)%m_size;
    39. }
    40. T pop() throw(bad_exception)
    41. {
    42. if(isEmpty())
    43. {
    44. throw bad_exception();
    45. }
    46. T tmp = m_data[m_front];
    47. m_front = (m_front + 1)%m_size;
    48. return tmp;
    49. }
    50. };

    3.2 测试程序

    1. int main()
    2. {
    3. CycleQueue<int> q(5);
    4. q.push(1);
    5. q.push(2);
    6. q.push(3);
    7. q.push(4);
    8. for (int i = 0; i < 4 ; i++)
    9. cout << q.pop() << endl;
    10. q.push(5);
    11. q.push(5);
    12. q.push(5);
    13. cout << q.pop() << endl;
    14. cout << q.pop() << endl;
    15. cout << q.pop() << endl;
    16. return 0;
    17. }

    4. 加锁优化实现

    4.1 模板类实现

    1. #include
    2. #include
    3. using namespace std;
    4. template <class T>
    5. class CycleQueue
    6. {
    7. private:
    8. QMutex m_mutex;
    9. unsigned int m_size;
    10. unsigned int m_front;
    11. unsigned int m_rear;
    12. T* m_data;
    13. public:
    14. CycleQueue(unsigned size)
    15. :m_size(size),
    16. m_front(0),
    17. m_rear(0)
    18. {
    19. m_data = new T[size];
    20. }
    21. ~CycleQueue()
    22. {
    23. delete [] m_data;
    24. }
    25. bool isEmpty()
    26. {
    27. return m_front == m_rear;
    28. }
    29. bool isFull()
    30. {
    31. return m_front == (m_rear + 1)%m_size;
    32. }
    33. bool push(T element)
    34. {
    35. bool bRet = false;
    36. m_mutex.lock();
    37. if(!isFull())
    38. {
    39. m_data[m_rear] = element;
    40. m_rear = (m_rear + 1) % m_size;
    41. bRet = true;
    42. }
    43. m_mutex.unlock();
    44. return bRet;
    45. }
    46. bool pop(T& element)
    47. {
    48. bool bRet = false;
    49. m_mutex.lock();
    50. if(!isEmpty())
    51. {
    52. element = m_data[m_front];
    53. m_front = (m_front + 1) % m_size;
    54. bRet = true;
    55. }
    56. m_mutex.unlock();
    57. return bRet;
    58. }
    59. };

    4.2 环形队列测试程序

    1. const int MAXQUEUE = 6;
    2. int main()
    3. {
    4. int ntmp;
    5. CycleQueue<int> q(MAXQUEUE + 1);
    6. cout << "Queue with max :" << MAXQUEUE << " Space is created!"<< endl;
    7. for (int i = 0; i < 8 ; i++)
    8. {
    9. bool bres = q.push(i);
    10. if(bres)
    11. cout << "enqueue:" << i << endl;
    12. else {
    13. cout << "queue is full!" << endl;
    14. }
    15. }
    16. for (int i = 0; i < 4 ; i++)
    17. {
    18. bool bres = q.pop(ntmp);
    19. if(bres)
    20. cout << "dequeue:" << ntmp << endl;
    21. else {
    22. cout << "queue is empty!" << endl;
    23. }
    24. }
    25. for (int i = 0; i < 8 ; i++)
    26. {
    27. bool bres = q.push(i);
    28. if(bres)
    29. cout << "enqueue:" << i << endl;
    30. else {
    31. cout << "queue is full!" << endl;
    32. }
    33. }
    34. for (int i = 0; i < 10 ; i++)
    35. {
    36. bool bres = q.pop(ntmp);
    37. if(bres)
    38. cout << "dequeue:" << ntmp << endl;
    39. else {
    40. cout << "queue is empty!" << endl;
    41. }
    42. }
    43. return 0;
    44. }

    4.3 运行结果

     

  • 相关阅读:
    Mysql双机热备配置方案原理及实战
    《程序员必备品质》——沉稳1
    氨基NH2/羧基COOH修饰/偶联/接枝NaY(Gd/Lu/Nd)F4:Yb,Tm核壳上转换纳米粒子
    android毕业设计选题基于Uniapp+SSM实现的互联网云数据环境下的供销APP购物商城电商
    不同的子序列 -- 动规
    postgresql
    如何解决DNS解析错误故障
    Unity插件Obi.Rope详解
    使用navicat模型功能 快速理清表间关系
    在Python中实现一个简单的社交媒体应用
  • 原文地址:https://blog.csdn.net/zkmrobot/article/details/127789046