Skip to content

Commit ce33183

Browse files
authored
Update implement-queue-using-stacks.cpp
1 parent f8c3ff5 commit ce33183

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

C++/implement-queue-using-stacks.cpp

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
// Time: O(1), amortized
22
// Space: O(n)
33

4-
class Queue {
4+
class MyQueue {
55
public:
6-
// Push element x to the back of queue.
6+
MyQueue() {
7+
8+
}
9+
710
void push(int x) {
811
A_.emplace(x);
912
}
1013

11-
// Removes the element from in front of queue.
12-
void pop(void) {
14+
int pop(void) {
1315
peek();
16+
int result = B_.top();
1417
B_.pop();
18+
return result;
1519
}
1620

17-
// Get the front element.
1821
int peek(void) {
1922
if (B_.empty()) {
2023
// Transfers the elements in A_ to B_.
@@ -29,11 +32,10 @@ class Queue {
2932
return B_.top();
3033
}
3134

32-
// Return whether the queue is empty.
3335
bool empty(void) {
3436
return A_.empty() && B_.empty();
3537
}
3638

3739
private:
38-
stack<int> A_, B_;
40+
stack<int> A_, B_;
3941
};

0 commit comments

Comments
 (0)