Step 1: Track elements removed so far
After the initial insertions, both structures contain the same elements:
Stack (bottom → top): [1, 5, 7, 8, 9, 2]
Queue (front → rear): [1, 5, 7, 8, 9, 2]
Step 2: Apply the first removals
Removing from the stack eliminates the most recent element:
Stack becomes [1, 5, 7, 8, 9]
Removing from the queue eliminates the earliest element:
Queue becomes [5, 7, 8, 9, 2]
Step 3: Apply the next pair of removals
Next stack removal deletes 9:
Stack → [1, 5, 7, 8]
Next queue removal deletes 5:
Queue → [7, 8, 9, 2]
Step 4: Transfer remaining queue elements to the stack
Each remaining queue element is moved to the stack in order:
Queue is now empty.
Step 5: Final removals from the stack
Removing the top element deletes 2:
Stack → [1, 5, 7, 8, 7, 8, 9]
Removing once more deletes 9:
Stack → [1, 5, 7, 8, 7, 8]
Final Answer:
Top element of the stack = 8
| LIST I | LIST II |
|---|---|
| (A) Circular Linked List | (I) Recursive Function Calls |
| (B) Doubly Linked List | (II) Round Robin Queue in CPU |
| (C) Stack | (III) Hash Tables |
| (D) Singly Linked List | (IV) Undo and Redo Functionality |