We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent f8c3ff5 commit ce33183Copy full SHA for ce33183
C++/implement-queue-using-stacks.cpp
@@ -1,20 +1,23 @@
1
// Time: O(1), amortized
2
// Space: O(n)
3
4
-class Queue {
+class MyQueue {
5
public:
6
- // Push element x to the back of queue.
+ MyQueue() {
7
+
8
+ }
9
10
void push(int x) {
11
A_.emplace(x);
12
}
13
- // Removes the element from in front of queue.
- void pop(void) {
14
+ int pop(void) {
15
peek();
16
+ int result = B_.top();
17
B_.pop();
18
+ return result;
19
20
- // Get the front element.
21
int peek(void) {
22
if (B_.empty()) {
23
// Transfers the elements in A_ to B_.
@@ -29,11 +32,10 @@ class Queue {
29
32
return B_.top();
30
33
31
34
- // Return whether the queue is empty.
35
bool empty(void) {
36
return A_.empty() && B_.empty();
37
38
39
private:
- stack<int> A_, B_;
40
+ stack<int> A_, B_;
41
};
0 commit comments