You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Implement the following operations of a stack using queues.
6
+
7
+
push(x) -- Push element x onto stack.
8
+
pop() -- Removes the element on top of the stack.
9
+
top() -- Get the top element.
10
+
empty() -- Return whether the stack is empty.
11
+
Example:
12
+
13
+
MyStack stack = new MyStack();
14
+
15
+
stack.push(1);
16
+
stack.push(2);
17
+
stack.top(); // returns 2
18
+
stack.pop(); // returns 2
19
+
stack.empty(); // returns false
20
+
Notes:
21
+
22
+
You must use only standard operations of a queue -- which means only push to back, peek/pop from front, size, and is empty operations are valid.
23
+
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
24
+
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
0 commit comments