Stack using queue
Posted: 14 Oct, 2020
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)
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.
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.
You are not required to print anything explicitly. It has already been taken care of. Just implement the function.
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
- 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.
Next Greater Element - I
Posted: 26 Dec, 2021
Posted: 18 Jan, 2022
Posted: 19 Jan, 2022
Posted: 20 Jan, 2022
Posted: 22 Jan, 2022