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
Be the first one to answer
Add answer anonymously...
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app