recategorized by
11,899 views
39 39 votes
A queue $Q$ containing $n$ items and an empty stack $S$ are given. It is required to transfer all the items from the queue to the stack, so that the item at the front of queue is on the TOP of the stack, and the order of all other items are preserved. Show how this can be done in $O(n)$ time using only a constant amount of additional storage. Note that the only operations which can be performed on the queue and stack are Delete, Insert, Push and Pop. Do not assume any implementation of the queue or stack.

5 Answers

Best answer
76 76 votes

We can do this be first extracting items one by one from $Q$, and inserting them to $S$. After all items are done, $S$ will contain the items in reverse order. Now, pop the elements from $S$ and insert to $Q$. After this operation, items in $Q$ will be in reverse order from the starting. Now, extract items from $Q$ and push on to stack and we are done. 

Do
Delete an item from Q
Push the item to S
While (! empty Q); 
Do
Pop an item from S
Insert the item to Q
While (! empty S); 
Do
Delete an item from Q
Push the item to S
While (! empty Q); 
edited by
9 9 votes
The given requirement can be achieved by,

1.Transfer the n elements from queue  Q to  stack S. Now, elements present in stack is in reverse order. These step require n iterations.

2. Transfer the n elements present in stack to queue one by one using pop(S) operation.These step require n iterations.

3. Perform Step 1.

So total iteration = n+n+n = 3n = O(n).
8 8 votes

"the item at the front of queue is on the TOP of the stack, and the order of all other items are preserved"

To do this we need an additional stack of size 2 apart from input Queue (Q) and Stack (S). Let's say our new stack (of size 2) is S₂.
1. First, we have to take the 1st element of the queue and put it on to the stack S₂ and keep there.
2. We have to do step 3 and 4 in a loop until Queue (Q) will be empty.
3. We have to take the next element from the queue (Q) and put it on to the stack S₂ and pop immediately.
4. Store that element in the stack S.
5. In the end, we have to pop the first element from the stack S₂ and put on to the Stack S.


To do this time taken will be O(n) and only a constant amount of space is required.

Hope this answer helps...!!!🙂


 

edited by
1 1 vote

"the order of all other items are preserved"   means

 When A is at the top it's next should be 'B' and then 'C'   i.e   Order in which queue items are present, They should be present in the same order in stack. 

Example: ABCDEF is present in the queue from front to rear ( A is front and F is at rear)


Step 1 : Delete A from Queue and push it to stack and pop it from stack and add the popped element to the end of the queue.
As a result,  You'll get BCDEFA

Step 2 : push all these elements to stack 
Now stack contains BCDEFA (from bottom to top)

Step 3 : pop out all elements one by one from the stack and  in the meanwhile insert them in the queue

Now your queue contain AFEDCB

Step 4 : Now again Delete A from Queue and push it to stack and pop it from stack and add the popped element to the end of the queue.

Now your queue contains FEDCBA

Step 5 :  Now deque all elements one by one and push them into the stack in the same order as they are dequeued

Finally Your Stack consists of ABCDEF from Top to Bottom 
This can be done in O(n) time and we are not using any additional storage other than what they've given in the question.

Position:
Show:

Related questions

49 49 votes
7 answers 7 answers
42.1k
42.1k views
Kathleen asked Oct 4, 2014
42,118 views
Which of the following permutations can be obtained in the output (in the same order) using a stack assuming that the input is the sequence $\text{1, 2, 3, 4, 5}$ in that...
35 35 votes
6 answers 6 answers
9.5k
9.5k views
Kathleen asked Oct 5, 2014
9,515 views
An array $A$ contains $n$ integers in non-decreasing order, $A \leq A \leq \cdots \leq A[n]$. Describe, using Pascal like pseudo code, a linear time algorithm to find $...
46 46 votes
3 answers 3 answers
14.2k
14.2k views
Kathleen asked Oct 5, 2014
14,233 views
A rooted tree with $12$ nodes has its nodes numbered $1$ to $12$ in pre-order. When the tree is traversed in post-order, the nodes are visited in the order $3, 5, 4, 2, 7...