forked from garvit-bhardwaj/Leetcode-Problems-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplement_queue_using_stack.cpp
More file actions
44 lines (39 loc) · 879 Bytes
/
Implement_queue_using_stack.cpp
File metadata and controls
44 lines (39 loc) · 879 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
class MyQueue {
stack<int> input;
stack<int> output;
public:
MyQueue() {
}
void push(int x) {
input.push(x);
}
int pop() {
if(!output.empty()) {
int x = output.top();
output.pop();
return x;
} else {
while(!input.empty()) {
output.push(input.top());
input.pop();
}
int x = output.top();
output.pop();
return x;
}
}
int peek() {
if(!output.empty()) {
return output.top();
} else {
while(!input.empty()) {
output.push(input.top());
input.pop();
}
return output.top();
}
}
bool empty() {
return input.empty() && output.empty();
}
};