-
Notifications
You must be signed in to change notification settings - Fork 77
/
solution.cpp
57 lines (53 loc) · 1.04 KB
/
solution.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
class Stack
{
public:
queue<int> queue1;
queue<int> queue2;
// Push element x onto stack.
void push(int x)
{
if(!queue1.empty())
{
queue1.push(x);
}
else
{
queue2.push(x);
}
}
// Removes the element on top of the stack.
void pop()
{
if(!queue1.empty())
{
while(queue1.size() > 1)
{
queue2.push(queue1.front());
queue1.pop();
}
queue1.pop();
}
else
{
while(queue2.size() > 1)
{
queue1.push(queue2.front());
queue2.pop();
}
queue2.pop();
}
}
// Get the top element.
int top()
{
if(!queue1.empty())
return queue1.back();
else
return queue2.back();
}
// Return whether the stack is empty.
bool empty()
{
return queue1.empty() && queue2.empty();
}
};