-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path225+MyStack.cpp
More file actions
52 lines (44 loc) · 1.02 KB
/
225+MyStack.cpp
File metadata and controls
52 lines (44 loc) · 1.02 KB
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
class MyStack {
public:
MyStack() {
}
void push(int x) {
mqT = mqO;
mqO = queue<int>(); // 置空
mqO.push(x); // 把x加入队首
while (mqT.size()) { // 把原来的结点,再加进去
int num = mqT.front(); mqT.pop();
mqO.push(num);
}
}
/*只用一个队列*/
void push2(int x) {
int size = mqO.size();
mqO.push(x);
for (int i = 0; i < size; ++i) {
mqO.push(mqO.front()); mqO.pop();
}
return;
}
int pop() {
int num = mqO.front(); mqO.pop();
return num;
}
int top() {
return mqO.front();
}
bool empty() {
return mqO.empty();
}
private:
queue<int> mqT;
queue<int> mqO;
};
/**
* Your MyStack object will be instantiated and called as such:
* MyStack* obj = new MyStack();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->top();
* bool param_4 = obj->empty();
*/