Stack using queue
Posted: 14 Oct, 2020
Difficulty: Moderate
Implement a Stack Data Structure specifically to store integer data using two Queues.
There should be two data members, both being Queues to store the data internally. You may use the inbuilt Queue.
Implement the following public functions :
1. Constructor:
It initializes the data members(queues) as required.
2. push(data) :
This function should take one argument of type integer. It pushes the element into the stack and returns nothing.
3. pop() :
It pops the element from the top of the stack and, in turn, returns the element being popped or deleted. In case the stack is empty, it returns -1.
4. top :
It returns the element being kept at the top of the stack. In case the stack is empty, it returns -1.
5. size() :
It returns the size of the stack at any given instance of time.
6. isEmpty() :
It returns a boolean value indicating whether the stack is empty or not.
Operations Performed on the Stack:
Query-1(Denoted by an integer 1): Pushes an integer data to the stack. (push function)
Query-2(Denoted by an integer 2): Pops the data kept at the top of the stack and returns it to the caller. (pop function)
Query-3(Denoted by an integer 3): Fetches and returns the data being kept at the top of the stack but doesn't remove it, unlike the pop function. (top function)
Query-4(Denoted by an integer 4): Returns the current size of the stack. (size function)
Query-5(Denoted by an integer 5): Returns a boolean value denoting whether the stack is empty or not. (isEmpty function)
Input Format:
The first line contains an integer 'Q’, which denotes the number of queries to be run against each operation in the stack.
The next 'Q' lines represent an operation that needs to be performed.
For the push operation, the input line will contain two integers separated by a single space, representing the type of the operation in integer and the integer data being pushed into the stack.
For the rest of the operations on the stack, the input line will contain only one integer value, representing the query being performed on the stack.
Output Format:
For Query-1, you do not need to return anything.
For Query-2, prints the data being popped from the stack.
For Query-3, print the data kept on the top of the stack.
For Query-4, print the current size of the stack.
For Query-5, print 'true' or 'false'(without quotes).
Output for every query will be printed in a separate line.
Note:
You are not required to print anything explicitly. It has already been taken care of. Just implement the function.
Constraints:
1 <= Q <= 1000
1 <= query type <= 5
-10^9 <= data <= 10^9 and data != -1
Where 'Q' is the total number of queries being performed on the stack and data represents the integer pushed into the stack.
Time Limit: 1 second
Approach 1
- This method ensures that every new element entered in the queue ‘q1’ is always at the front.
- Hence, during pop operation, we just dequeue from ‘q1’.
- For this, we need another queue ‘q2., which is used to keep every new element to the front of ‘q1’.
- During push operation :
- Enqueue new element ‘x’ to queue ‘q2’.
- One by one, dequeue everything from ‘q1’ and enqueue to ‘q2’.
- Swap the names of ‘q1’ and ‘q2’.
- During pop operation :
- Dequeue an element from ‘q1’ and return it.
- For the size function, return the size of queue ‘q1’ and for the empty function, check if ‘q1’ is empty.
Approach 2
- We will be using two queues, ‘q1’ and ‘q2’.
- In a push operation, the new element is always enqueued to ‘q1’.
- During push operation :
- Enqueue new element ‘x’ to ‘q1’.
- During pop operation :
- One by one, dequeue everything except the last element from ‘q1’ and enqueue to ‘q2’.
- Dequeue the last item of ‘q1’, and the dequeued item is the result, store it.
- Swap the names of ‘q1’ and ‘q2’.
- Return the item stored in step 2.
- For the size function, return the size of queue ‘q1’ and for the empty function, check if ‘q1’ is empty.
Approach 3
- We will be using a single queue ‘q1’.
- In a push operation, we can calculate the size of the queue ‘q1’.Hence we enqueue new data to the queue.
- Now suppose before inserting new data size of the queue is ‘x’, Hence we dequeue ‘x’ elements from the queue and push it back again into the same queue.
- This would push the new element to the front of the queue.
- During pop and top operation :
- The element we are searching for is the front of the queue.
- Hence during pop operation simply access the front of the queue and remove the element.
- During top operation simply access the front element of the queue.
- Hence assuming we know the size of the queue at any instance we can solve this problem using a single queue.
SIMILAR PROBLEMS
Next Greater Element - I
Posted: 26 Dec, 2021
Difficulty: Moderate
Preorder Traversal
Posted: 18 Jan, 2022
Difficulty: Easy
Inorder Traversal
Posted: 19 Jan, 2022
Difficulty: Easy
Postorder Traversal
Posted: 20 Jan, 2022
Difficulty: Easy
Min Stack
Posted: 22 Jan, 2022
Difficulty: Easy