Implement Stack using Queue
lc225 Implement Stack using Queues ($\leftarrow$ click)
Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).
Implement a last-in-first-out (LIFO) stack using only one queue.
1 | class MyStack: |
Implement Queue using Stacks
lc 232 Implement Queue using Stacks ($\leftarrow$ click)
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty).
1 | class MyQueue: |
Note: The peek() and pop() functions require a total reversal of N elements in N consecutive front element deletion operations, leading to an amortized time complexity of O(1).