当前位置:   article > 正文

【数据结构】顺序队列的实现(c++)_c++实现数据结构—顺序队列

c++实现数据结构—顺序队列

头文件:


  1. #pragma once
  2. #include <iostream>
  3. #include <assert.h>
  4. using namespace std;
  5. template<class Type>
  6. class SeqQueue
  7. {
  8. public:
  9. SeqQueue(size_t sz = INIT_SZ);
  10. ~SeqQueue();
  11. public:
  12. bool empty()const;
  13. bool full()const;
  14. void show()const;
  15. bool push(const Type &x);
  16. bool pop();
  17. void gettop(Type &x);
  18. int length()const;
  19. void clear();
  20. void destory();
  21. void quit_system(Type &x);
  22. private:
  23. enum{ INIT_SZ = 8 };
  24. Type *base;
  25. int capacity;
  26. int head;
  27. int tail;
  28. };
  29. template<class Type>
  30. SeqQueue<Type>::SeqQueue(size_t sz = INIT_SZ)
  31. {
  32. capacity = sz > INIT_SZ ? sz : INIT_SZ;
  33. base = new Type[capacity];
  34. assert(base != NULL);
  35. head = 0;
  36. tail = 0;
  37. }
  38. template<class Type>
  39. SeqQueue<Type>::~SeqQueue()
  40. {
  41. destory();
  42. }
  43. // 判断队列是否满了,顺序队列呈现虚满的状态
  44. template<class Type>
  45. bool SeqQueue<Type>::full()const
  46. {
  47. return (tail >
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/824396
推荐阅读
相关标签
  

闽ICP备14008679号