in DS recategorized by
497 views
1 vote
1 vote
Five items $\text{A, B, C, D, E}$ are pushed onto a stack, one after other starting from item $\mathrm{A}$. The stack is then popped by three items, and each item is inserted into a queue. Next, two items are deleted from the queue, and the deleted items are pushed back onto the stack. Now, one item is popped from the stack. Which item is at the top of the stack.
in DS recategorized by
by
497 views

1 Answer

0 votes
0 votes
After the five items are pushed onto the stack, the stack looks like this, with the top of the stack at the right:

A B C D E

After three items are popped from the stack and inserted into the queue, the queue looks like this, with the front of the queue at the left:

C D E

After two items are deleted from the queue and pushed back onto the stack, the stack looks like this:

E C

Finally, if one item is popped from the stack, the item at the top of the stack will be "C".

3 Comments

Which element is first inserted to the queue?
0
0
In the sequence of operations that you described, the element that is first inserted into the queue is "A"
0
0

The answer should be E as:

  1. Stack looks like ABCDE with E as the TOS.
  2. 3 items are deleted from stack so we have EDC which are pushed into queue so queue looks like EDC with front pointer pointing to E.
  3. 2 items are deleted from the queue i.e E and D which are then pushed onto the stack so stack looks like ABED with D as the TOS.
  4. 1 item is poped from stack so D is poped leaving E as the TOS.
1
1

Related questions