Implement Queue Using Stacks
Implement a FIFO queue using stacks.
Statement
Given a Stack
class with common operations such as push()
, pop()
, and isEmpty()
, implement a Queue
class with its enqueue()
, dequeue()
, and isEmpty()
operations. The isEmpty()
function should return true
if the stack is empty and false
otherwise.
Note: If the queue is empty, the
dequeue()
function should return .
A stack is a data structure in which objects are inserted and removed according to the
push()
: Inserts an item at the top of the stack.pop()
: Removes an item from the top of the stack and returns it.
A queue is a data structure in which objects are inserted and removed according to the
enqueue()
: Inserts an item at the back of the queue.dequeue()
: Removes an item from the front of the queue and returns it.
Example
Sample input
Enqueue(3), Dequeue(), Dequeue(), Enqueue(1), Enqueue(7), Dequeue()
Expected output
3, -1, 1
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.