Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8402b2c

Browse files
authored
Update 5.4.1栈实现.md
1 parent a845045 commit 8402b2c

File tree

1 file changed

+63
-63
lines changed

1 file changed

+63
-63
lines changed

‎5.4.1栈实现.md‎

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -25,68 +25,68 @@
2525

2626
代码实现:
2727

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;
7054
}
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;
9164
}
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /