-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathImplement_Queue_by_Stacks.cpp
52 lines (46 loc) · 1.29 KB
/
Implement_Queue_by_Stacks.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
/*
As the title described, you should only use two stacks to implement a queue's actions. The queue should support push(element), pop() and top() where pop is pop the first(a.k.a front) element in the queue. Both pop and top methods should return the value of first element.
Example
For push(1), pop(), push(2), push(3), top(), pop(), you should return 1, 2 and 2
*/
#include <stack>
using namespace std;
class Queue {
public:
stack<int> stack1;
stack<int> stack2;
Queue() {
// do intialization if necessary
}
void push(int element) {
// write your code here
stack1.push(element);
}
int pop() {
// write your code here
while (!stack1.empty()) {
stack2.push(stack1.top());
stack1.pop();
}
int result = stack2.top();
stack2.pop();
while (!stack2.empty()) {
stack1.push(stack2.top());
stack2.pop();
}
return result;
}
int top() {
// write your code here
while (!stack1.empty()) {
stack2.push(stack1.top());
stack1.pop();
}
int result = stack2.top();
while (!stack2.empty()) {
stack1.push(stack2.top());
stack2.pop();
}
return result;
}
};