求一个栈的C++实现代码!

2024-12-04 00:51:26
推荐回答(1个)
回答1:

#include
2#include
3using namespace std;
4 class Stack
5 {
6 public:
7 inline bool empty();
8 bool full();
9 bool push( int elem);
10 int pop();
11 void getmem();
12 int size(){return _stack.size();}
13 private:
14 vector _stack;
15 };
16 inline bool Stack::empty()
17 {
18 return _stack.empty();
19 }
20
21 inline bool Stack::full()
22 {
23 return _stack.size()==_stack.max_size();
24 }
25 bool Stack:: push(int elem)
26 {
27 if(full())
28 return false;
29 _stack.push_back(elem);
30 return true;
31 }
32
33 int Stack:: pop( )
34 {
35 if(empty())
36 return false;
37 int elem=_stack.back();
38 _stack.pop_back();
39 return elem;
40 }
41 void Stack::getmem()
42 {
43 if(empty())
44 return ;
45 cout<<_stack[0];
46 for(int ix=1;ix!=_stack.size();++ix)
47 cout<<' '<<_stack[ix];
48 cout<49 }
50int main()
51{
52 Stack s;
53 cout<54 for(int i=0,j=1;i<1000;)
55 {
56 s.push(i+j);
57 int k=i;
58 i=j;
59 j=k+i;
60 }
61 cout<62 s.getmem();
63 s.pop();
64 s.pop();
65 s.getmem();
66
67 return 0;
68}