-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path225.cpp
82 lines (72 loc) · 1.65 KB
/
225.cpp
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <queue>
using namespace std;
class MyStack {
public:
queue<int> queue1;
queue<int> queue2;
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
queue1.push(x);
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int ans;
if (queue1.size()==0)
return false;
while (!queue1.empty())
{
if(queue1.size()==1)
{
ans=queue1.front();
queue1.pop();
break;
}
queue2.push(queue1.front());
queue1.pop();
}
while (!queue2.empty())
{
queue1.push(queue2.front());
queue2.pop();
}
return ans;
}
/** Get the top element. */
int top() {
int ans;
if (queue1.size()==0)
return false;
while (!queue1.empty())
{
queue2.push(queue1.front());
ans=queue1.front();
queue1.pop();
}
while (!queue2.empty())
{
queue1.push(queue2.front());
queue2.pop();
}
return ans;
}
/** Returns whether the stack is empty. */
bool empty() {
return queue1.empty();
}
};
/**
* 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();
*/
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}