写栈比队列更简单一些,毕竟只有一个数据出入口。

之前用C在程序里模拟栈代替递归的时候,直接搞个数组来实现都是非常轻松愉快的事情。

不多说,放代码。

 

测试代码

 1 #include <iostream>
 2 #include "stack.h"
 3 using namespace std;
 4 using namespace stack;
 5 template class Stack<int>;
 6 int main(void)
 7 {
 8     Stack<int> number;
 9 
10     //测试入栈
11     cout << "/*additem()*/" << endl;
12     number.push(2);
13     number.push(3);
14     number.push(5);
15     number.push(7);
16     number.push(11);
17     number.traverse();
18     cout << "\n/*end*/\n\n" << flush;
19 
20     //测试获取长度
21     cout << "/*length()*/" << endl;
22     cout << number.size() << endl;
23     cout << "/*end*/\n\n" << flush;
24 
25     //测试获得头元素
26     cout << "/*getfirst()*/" << endl;
27     cout << number.getfirst() << endl;
28     cout << "/*end*/\n\n" << flush;
29 
30     //测试出栈
31     cout << "/*remove()*/" << endl;
32     number.pop();
33     number.pop();
34     number.traverse();
35     cout << "\n/*end*/\n\n" << flush;
36 
37     //测试清空,并测试从空表中出栈
38     cout << "/*clear(),remove()*/" << endl;
39     number.clear();
40     number.pop();
41     cout << "/*end*/\n\n" << flush;
42 
43     system("pause");
44 }
View Code

相关文章:

  • 2022-01-25
  • 2021-07-08
  • 2021-10-16
  • 2021-08-02
  • 2021-08-09
  • 2022-02-18
  • 2021-10-26
  • 2021-11-25
猜你喜欢
  • 2021-12-21
  • 2021-10-08
  • 2022-01-19
  • 2021-09-19
  • 2021-06-04
  • 2022-02-13
  • 2022-01-18
相关资源
相似解决方案