Queue Using Stack Problem Statement
Implement a queue data structure that follows the FIFO (First In First Out) property, using only instances of the stack data structure.
Note:
- Complete predefined functions that mimic the behavior of a normal queue to handle input queries efficiently.
- The implemented queue must support the following operations:
enQueue(data)
: Adds an integer to the back of the queue.deQueue()
: Removes and returns the integer from the front of the queue. Returns -1 if the queue is empty.peek()
: Returns the element at the front of the queue without removing it. Returns -1 if the queue is empty.isEmpty()
: Returns true if the queue is empty, false otherwise.- You will receive
q
queries of 4 types: 1 val
: Insert the integerval
to the back of the queue.2
: Remove and return the element from the front of the queue.3
: Return the element at the front without removing it.4
: Return true if the queue is empty, false otherwise.
Input:
The first line contains an integer 'T', the number of queries.
Each of the next 'T' lines contains a query as specified in the problem statement.
Output:
No output needed for Query-1.
For Query-2, return the deQueued integer.
For Query-3, return the integer at the front of the queue.
For Query-4, return “true” if the queue is empty, “false” otherwise.
Note:
- You are not required to print the output.
- Implement the required functions using stack operations only (e.g., push, pop).
- You may use built-in stack data structures available in languages like C++, Java.
- Assume the queue's capacity is effectively infinite.
Constraints:
1 ≤ T ≤ 1000
1 ≤ type ≤ 4
1 ≤ data ≤ 10^9

AnswerBot
4mo
Implement a queue using only stack data structure with predefined functions for FIFO operations.
Use two stacks to simulate a queue - one for enqueue and one for dequeue operations.
For enQueue operatio...read more
Help your peers!
Add answer anonymously...
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

