Interchange elements of Stack and Queue without changing order
Given a stack St of M elements and a queue Q of N elements. The task is to put every element of stack into the queue and every element of the queue into the stack without changing their order.
Examples:
Input: St = {4, 3, 2, 1}, Q = {8, 7, 6, 5}
Output: St = {8, 7, 6, 5}, Q = {1, 2, 3, 4}Input: St = {0, 1}, Q = {2, 3}
Output: St = {2, 3}, Q = {0, 1}
Naive Approach: The basic approach is to store the elements of the stack and the queue in a separate array and then put them again into the queue and the stack.
Time Complexity: O(N + M)
Auxiliary Space: O(N + M)
Optimized Approach: This problem can be solved without using any extra space by using the last-in-first-out and first-in-first-out properties of stack and queue.
Follow the steps to solve the problem:
- Put every element of the queue into the stack.
- Put extra elements of the stack into the queue again, the extra element of the stack is the element coming from the queue. Now, the queue is reversed.
- Now, put every element of the stack into the queue.
- At last, put original elements of the queue into the stack.
- Now repeat the first and second step again to maintain the order of the stack elements in the queue.
Follow the illustrations below for a better understanding of the approach:
Illustrations:
Consider the stack = [1, 2, 3, 4] where 1 is at the top and the queue = [8, 7, 6, 5] where 8 is at the front.
In first step:
One by one pop the element from the queue(i.e., all the elements of queue) and push into stack.
After First Iteration, Stack = [8, 1, 2, 3, 4] and queue = [7, 6, 5]
After Second Iteration, Stack = [7, 8, 1, 2, 3, 4] and queue = [6, 5]
After Third Iteration, Stack = [6, 7, 8, 1, 2, 3, 4] and queue = [5]
After Fourth Iteration, Stack = [5, 6, 7, 8, 1, 2, 3, 4] and queue = []In second step:
One by one pop the element from the stack(i.e., coming from queue) and push into queue.
After First Iteration, Stack = [6, 7, 8, 1, 2, 3, 4] and queue = [5]
After Second Iteration, Stack = [7, 8, 1, 2, 3, 4] and queue = [5, 6]
After Third Iteration, Stack = [8, 1, 2, 3, 4] and queue = [5, 6, 7]
After Fourth Iteration, Stack = [1, 2, 3, 4] and queue = [5, 6, 7, 8]In third step:
One by one pop the element from the stack(i.e., remaining all the elements) and push into queue.
After First Iteration, Stack = [2, 3, 4] and queue = [5, 6, 7, 8, 1]
After Second Iteration, Stack = [3, 4] and queue = [5, 6, 7, 8, 1, 2]
After Third Iteration, Stack = [4] and queue = [5, 6, 7, 8, 1, 2, 3]
After Fourth Iteration, Stack = [] and queue = [5, 6, 7, 8, 1, 2, 3, 4]In fourth step:
One by one pop the element from the queue(i.e., only element of queue before first step) and push into stack.
After First Iteration, Stack = [5] and queue = [6, 7, 8, 1, 2, 3, 4]
After Second Iteration, Stack = [6, 5] and queue = [7, 8, 1, 2, 3, 4]
After Third Iteration, Stack = [7, 6, 5] and queue = [8, 1, 2, 3, 4]
After Fourth Iteration, Stack = [8, 7, 6, 5] and queue = [1, 2, 3, 4]Now repeat first and second step.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to Put every element of stack// into queue and queue into stack// without changing its ordervoid changeElement(stack<int>& St, queue<int>& Q){ // Calculate size of queue Q int Size = Q.size(); int Temp = Size; int N = St.size(); // Put every element of queue into stack while (!Q.empty()) { St.push(Q.front()); Q.pop(); } // Put extra element of stack into // queue again, extra element of stack // is the element coming from queue. // Now, the queue is reversed while (Size != 0) { Q.push(St.top()); St.pop(); Size--; } // Put every element of stack into queue while (!St.empty()) { Q.push(St.top()); St.pop(); } Size = Temp; // Put initial element of queue // into stack while (Size != 0) { St.push(Q.front()); Q.pop(); Size--; } // Repeat the first and second steps while (!Q.empty()) { St.push(Q.front()); Q.pop(); } while (N != 0) { Q.push(St.top()); St.pop(); N--; }}// Function to traverse till stack is// not empty and print the element in itvoid printStack(stack<int>& St){ while (!St.empty()) { cout << St.top() << " "; St.pop(); } cout << endl;}// Function to traverse till queue is not// empty and print the element in itvoid printQueue(queue<int>& Q){ while (!Q.empty()) { cout << Q.front() << " "; Q.pop(); } cout << endl;}// Driver Codeint main(){ stack<int> St; queue<int> Q; // Fill element into stack St.push(4); St.push(3); St.push(2); St.push(1); // Fill element into queue Q.push(8); Q.push(7); Q.push(6); Q.push(5); changeElement(St, Q); cout << "Stack = "; printStack(St); cout << "Queue = "; printQueue(Q); return 0;} |
Python3
# python3 program for the above approachimport collections# Function to Put every element of stack# into queue and queue into stack# without changing its orderdef changeElement(St, Q): # Calculate size of queue Q Size = len(Q) Temp = Size N = len(St) # Put every element of queue into stack while (len(Q) != 0): St.append(Q.popleft()) # Put extra element of stack into # queue again, extra element of stack # is the element coming from queue. # Now, the queue is reversed while (Size != 0): Q.append(St.pop()) Size -= 1 # Put every element of stack into queue while (len(St) != 0): Q.append(St.pop()) Size = Temp # Put initial element of queue # into stack while (Size != 0): St.append(Q.popleft()) Size -= 1 # Repeat the first and second steps while (len(Q) != 0): St.append(Q.popleft()) while (N != 0): Q.append(St.pop()) N -= 1# Function to traverse till stack is# not empty and print the element in itdef printStack(St): while (len(St) != 0): print(St.pop(), end=" ") print()# Function to traverse till queue is not# empty and print the element in itdef printQueue(Q): while (len(Q) != 0): print(Q.popleft(), end=" ") print()# Driver Codeif __name__ == "__main__": St = collections.deque() Q = collections.deque() # Fill element into stack St.append(4) St.append(3) St.append(2) St.append(1) # Fill element into queue Q.append(8) Q.append(7) Q.append(6) Q.append(5) changeElement(St, Q) print("Stack = ", end="") printStack(St) print("Queue = ", end="") printQueue(Q) # This code is contributed by rakeshsahni |
Stack = 8 7 6 5 Queue = 1 2 3 4
Time Complexity: O(M+N)
Auxiliary Space: O(1)

