数据结构——顺序双栈模板类实现

ゞ 浴缸里的玫瑰 2022-06-07 06:44 262阅读 0赞

数据结构3.1.2
顺序栈中单栈和双栈(数组的两端分别为栈底)其实极为相似,道理上是一样的,实现的时候只需要多定义一套top标记和bottom标记即可,在判断栈FULL的时候,即是判断两个top是否相遇,这里我用两个元素的数组来定义两个栈顶标记,下面贴出代码(实现方式是多种多样的,如有不当,万望指正)^ _ ^
模板类代码:

  1. #include <iostream>
  2. #include <cassert>
  3. using namespace std;
  4. template<class T>
  5. class DBstack {
  6. public:
  7. DBstack(int sz = 100); //构造函数/每个栈的起始容量是50
  8. ~DBstack(); //析构函数
  9. bool Push(T & x, int d); //将x推入d代表的栈中,d = 0是1号栈否则为2号栈
  10. bool Pop(T & x, int d); //将d号栈顶元素弹出,其值返回在引用x当中,d = 0是1号栈否则为2号栈
  11. bool getTop(T & x, int d); //得到d号栈的栈顶元素,其值返回带x的引用当中(不弹出)
  12. bool isEmpty(int d)const; //判断d号栈是否为NULL
  13. bool isFull()const; //判断栈是否为满
  14. int getSize(int d)const; //返回d号栈当前元素的个数
  15. void makeEmpty(int d); //清空d号栈
  16. void output(int d)const; //输出d号栈的内容
  17. private:
  18. T *stack; //双栈数组
  19. int top[2]; //分别是两个栈的栈顶指针
  20. int bottom[2]; //两个栈的栈底指针
  21. void overflowProcess(); //栈的溢出处理
  22. };
  23. //函数定义
  24. template<class T>
  25. DBstack<T>::DBstack(int sz) {
  26. //构造函数
  27. stack = new T[sz]; //开辟双栈数组
  28. assert(stack != NULL); //中断处理
  29. top[0] = bottom[0] = -1;
  30. top[1] = bottom[1] = sz;
  31. }
  32. template<class T>
  33. DBstack<T>::~DBstack() {
  34. //析构函数,释放程序中的资源
  35. delete[] stack;
  36. }
  37. template<class T>
  38. bool DBstack<T>::Push(T & x, int d) {
  39. //将x推入d代表的栈中,d = 0是1号栈否则为2号栈
  40. if (top[0] + 1 == top[1]) { //再插入即两个top相遇
  41. return false; //栈满,存储失败
  42. }
  43. if (0 == d) {
  44. //推入1号栈
  45. stack[++top[0]] = x;
  46. }
  47. else {
  48. stack[--top[1]] = x;
  49. }
  50. return true;
  51. }
  52. template<class T>
  53. bool DBstack<T>::Pop(T & x, int d) {
  54. //将d号栈顶元素弹出,其值返回在引用x当中,d = 0是1号栈否则为2号栈
  55. if (isEmpty(d)) { //检查栈是否为NULL
  56. return false;
  57. }
  58. //执行弹出操作
  59. if (0 == d) {
  60. x = stack[top[0]--];
  61. }
  62. else {
  63. x = stack[top[1]++];
  64. }
  65. return true;
  66. }
  67. template<class T>
  68. bool DBstack<T>::getTop(T & x, int d) {
  69. //得到d号栈的栈顶元素,其值返回带x的引用当中(不弹出)
  70. if (0 == d) {
  71. x = stack[top[0]];
  72. return true;
  73. }
  74. else {
  75. x = stack[top[1]];
  76. return true;
  77. }
  78. return false;
  79. }
  80. template<class T>
  81. bool DBstack<T>::isEmpty(int d)const {
  82. //判断d号栈是否为NULL
  83. if (0 == d) {
  84. if (top[0] == bottom[0]) {
  85. return true;
  86. }
  87. }
  88. else {
  89. if (top[1] == bottom[1]) {
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. template<class T>
  96. bool DBstack<T>::isFull()const {
  97. //判断栈是否为FULL
  98. if (top[0] == top[1]) {
  99. return true;
  100. }
  101. return false;
  102. }
  103. template<class T>
  104. int DBstack<T>::getSize(int d) const {
  105. //返回d号栈当前元素的个数
  106. if (0 == d) {
  107. return top[0] + 1;
  108. }
  109. else {
  110. return 100 - top[1];
  111. }
  112. }
  113. template<class T>
  114. void DBstack<T>::makeEmpty(int d) {
  115. //set NULL
  116. if (0 == d) {
  117. top[0] = bottom[0];
  118. }
  119. else {
  120. top[1] = bottom[1];
  121. }
  122. }
  123. template<class T>
  124. void DBstack<T>::output(int d)const {
  125. //输出任意栈的全部元素
  126. if (0 == d) {
  127. //输出1号栈的全部元素
  128. for (int i = bottom[0] + 1; i <= top[0]; i ++) {
  129. cout << stack[i] << ' ';
  130. }
  131. }
  132. else {
  133. //输出2号栈的全部元素
  134. for (int j = bottom[1] - 1; j >= top[1]; j --) {
  135. cout << stack[j] << ' ';
  136. }
  137. }
  138. cout << endl;
  139. }

main测试代码:

  1. int main()
  2. {
  3. //填充元素测试
  4. DBstack<int> doubl_stack; //创建一个双栈
  5. for (int i = 1; i <= 5; i ++) { //给0号栈填充上5个值
  6. doubl_stack.Push(i , 0);
  7. }
  8. for (int j = 1; j <= 7; j++) { //同上
  9. doubl_stack.Push(j, 1); //第二个参数非0即可
  10. }
  11. doubl_stack.output(0); //输出两个栈中的元素
  12. doubl_stack.output(1);
  13. //弹出元素测试
  14. int pop_1, pop_2, pop_3, pop_4;
  15. doubl_stack.Pop(pop_1, 0);
  16. doubl_stack.Pop(pop_2, 1);
  17. doubl_stack.Pop(pop_3, 1);
  18. doubl_stack.Pop(pop_4, 1);
  19. doubl_stack.output(0); //输出两个栈中的元素
  20. doubl_stack.output(1);
  21. //得到元素个数测试
  22. int add_1 = 100;
  23. doubl_stack.Push(add_1, 0);
  24. cout << doubl_stack.getSize(0) << endl;
  25. cout << doubl_stack.getSize(1) << endl;
  26. //set Empty测试
  27. int pop_5;
  28. doubl_stack.makeEmpty(0);
  29. doubl_stack.output(0);
  30. doubl_stack.output(1);
  31. doubl_stack.Pop(pop_5 ,0); //检测pop函数对Empty的处理是否正常
  32. system("pause");
  33. return 0;
  34. }

运行效果:
这里写图片描述

发表评论

表情:
评论列表 (有 0 条评论,262人围观)

还没有评论,来说两句吧...

相关阅读

    相关 数据结构-顺序

    / 编写一个程序,实现顺序栈(假设栈中元素类型为char)的各种基本运算。并完成下面功能: (1)初始化栈s; (2)判断栈s是否非空;

    相关 数据结构 顺序

    做一个豁达而努力的自己。 栈的定义:只能在表的一端(栈顶)进行插入和删除的线性表。 逻辑结构:与线性表相同,仍为一一对应的关系。 存储结构:用顺序栈和链栈存储均可,但以顺