-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_by_2_stack.cpp
More file actions
54 lines (40 loc) · 900 Bytes
/
list_by_2_stack.cpp
File metadata and controls
54 lines (40 loc) · 900 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* 常见题目,用两个栈来实现一个队列
*
* */
#include <stack>
#include <iostream>
using namespace std;
class mylist {
public:
mylist() {
}
private:
stack<int> mystack1;
stack<int> mystack2;
public:
void add_tail(int value) {
mystack1.push(value);
}
int get_head() {
while(!mystack1.empty()) {
mystack2.push(mystack1.top());
mystack1.pop();
}
int value = mystack2.top();
mystack2.pop();
while(!mystack2.empty()) {
mystack1.push(mystack2.top());
mystack2.pop();
}
return value;
}
};
int main() {
class mylist t;
t.add_tail(1);
t.add_tail(2);
t.add_tail(3);
cout<<"head "<<t.get_head()<<endl;
return 0L;
}