|
25 | 25 |
|
26 | 26 | 代码实现:
|
27 | 27 |
|
28 | | - ```cpp |
29 | | - // 单链表实现栈 |
30 | | - #include <iostream> |
31 | | - #include <forward_list> |
32 | | - using namespace std; |
33 | | - |
34 | | - class Stack{ |
35 | | - public: |
36 | | - void push(int x); // 入栈 |
37 | | - int pop(); // 出栈 |
38 | | - int top(); // 返回栈顶元素 |
39 | | - bool is_empty(); // 返回栈是否为空 |
40 | | - |
41 | | - private: |
42 | | - std::forward_list<int>::iterator p;// 栈顶指针 |
43 | | - std::forward_list<int> linklist;// 单链表 |
44 | | - }; |
45 | | - |
46 | | - void Stack::push(int x){ |
47 | | - this->linklist.push_front(x); |
48 | | - this->p = this->linklist.begin(); |
49 | | - } |
50 | | - |
51 | | - int Stack::pop(){ |
52 | | - if (this->linklist.empty()){ |
53 | | - return -1; |
54 | | - } |
55 | | - int ans = *this->p; |
56 | | - this->linklist.pop_front(); |
57 | | - this->p = this->linklist.begin(); |
58 | | - return ans; |
59 | | - } |
60 | | - |
61 | | - int Stack::top(){ |
62 | | - if (this->linklist.empty()){ |
63 | | - return -9999; |
64 | | - } |
65 | | - return *this->p; |
66 | | - } |
67 | | - |
68 | | - bool Stack::is_empty(){ |
69 | | - return this->linklist.empty(); |
| 28 | +```cpp |
| 29 | +// 单链表实现栈 |
| 30 | +#include <iostream> |
| 31 | +#include <forward_list> |
| 32 | +using namespace std; |
| 33 | + |
| 34 | +class Stack{ |
| 35 | +public: |
| 36 | + void push(int x); // 入栈 |
| 37 | + int pop(); // 出栈 |
| 38 | + int top(); // 返回栈顶元素 |
| 39 | + bool is_empty(); // 返回栈是否为空 |
| 40 | + |
| 41 | +private: |
| 42 | + std::forward_list<int>::iterator p;// 栈顶指针 |
| 43 | + std::forward_list<int> linklist;// 单链表 |
| 44 | +}; |
| 45 | + |
| 46 | +void Stack::push(int x){ |
| 47 | + this->linklist.push_front(x); |
| 48 | + this->p = this->linklist.begin(); |
| 49 | +} |
| 50 | + |
| 51 | +int Stack::pop(){ |
| 52 | + if (this->linklist.empty()){ |
| 53 | + return -1; |
70 | 54 | }
|
71 | | - |
72 | | - int main(){ |
73 | | - Stack stack; |
74 | | - std::cout<< stack.is_empty() << endl;// true |
75 | | - |
76 | | - stack.push(1); |
77 | | - std::cout<< stack.top() << endl;// 1 |
78 | | - |
79 | | - stack.push(2); |
80 | | - std::cout<< stack.top() << endl;// 2 |
81 | | - |
82 | | - stack.pop(); |
83 | | - std::cout<< stack.top() << endl;// 1 |
84 | | - std::cout<< stack.is_empty() << endl;// false |
85 | | - |
86 | | - stack.pop(); |
87 | | - std::cout<< stack.top() << endl;// -9999 |
88 | | - std::cout<< stack.is_empty() << endl;// true |
89 | | - |
90 | | - return 0; |
| 55 | + int ans = *this->p; |
| 56 | + this->linklist.pop_front(); |
| 57 | + this->p = this->linklist.begin(); |
| 58 | + return ans; |
| 59 | +} |
| 60 | + |
| 61 | +int Stack::top(){ |
| 62 | + if (this->linklist.empty()){ |
| 63 | + return -9999; |
91 | 64 | }
|
92 | | - ``` |
| 65 | + return *this->p; |
| 66 | +} |
| 67 | + |
| 68 | +bool Stack::is_empty(){ |
| 69 | + return this->linklist.empty(); |
| 70 | +} |
| 71 | + |
| 72 | +int main(){ |
| 73 | + Stack stack; |
| 74 | + std::cout<< (stack.is_empty() ? "true" : "false") << endl;// true |
| 75 | + |
| 76 | + stack.push(1); |
| 77 | + std::cout<< stack.top() << endl;// 1 |
| 78 | + |
| 79 | + stack.push(2); |
| 80 | + std::cout<< stack.top() << endl;// 2 |
| 81 | + |
| 82 | + stack.pop(); |
| 83 | + std::cout<< stack.top() << endl;// 1 |
| 84 | + std::cout<< (stack.is_empty() ? "true" : "false") << endl;// false |
| 85 | + |
| 86 | + stack.pop(); |
| 87 | + std::cout<< stack.top() << endl;// -9999 |
| 88 | + std::cout<< (stack.is_empty() ? "true" : "false") << endl;// true |
| 89 | + |
| 90 | + return 0; |
| 91 | +} |
| 92 | +``` |
0 commit comments