-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathstacks_with_queues.cpp
66 lines (47 loc) · 1.68 KB
/
stacks_with_queues.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
/*
This implementation maintains two queues, queue1 and queue2, where queue1 always holds the elements in the stack. When pushing a new element, it is added to queue2, and then all the elements from queue1 are moved to queue2, making the newly added element the front/top of the stack. Finally, the names of the two queues are swapped to maintain the order.
The pop() function removes and returns the front/top element of queue1, while the top() function returns the front/top element without removing it. Both functions check if queue1 is empty and throw an exception if the stack is empty.
The empty() function checks if queue1 is empty and returns true if it is, indicating an empty stack.
Time complexity
push(x): O(n)
pop(): O(1)
top(): O(1)
empty(): O(1)
*/
#include <queue>
class MyStack {
private:
std::queue<int> queue1;
std::queue<int> queue2;
public:
MyStack() {
}
void push(int x) {
// Add the new element to queue2
queue2.push(x);
// Move all elements from queue1 to queue2
while (!queue1.empty()) {
queue2.push(queue1.front());
queue1.pop();
}
// Swap the names of the two queues
std::swap(queue1, queue2);
}
int pop() {
if (queue1.empty()) {
throw std::runtime_error("Stack is empty");
}
int topElement = queue1.front();
queue1.pop();
return topElement;
}
int top() {
if (queue1.empty()) {
throw std::runtime_error("Stack is empty");
}
return queue1.front();
}
bool empty() {
return queue1.empty();
}
};