I was asked a question on how to use a pair of Queues to create a Stack and how to use a pair of Stacks to create a Queue. Any thoughts on how I would do this? Right now I don't even know where to start.
3 Answers
To answer the question in the comment above-
Two queues to implement a stack(Assume Queue A is filled with elements in LIFO order and Queue B is empty):
PUSH: Empty Queue A into Queue B. LIFO order will be persisted in Queue B. Now insert the new element into Queue A. Now empty Queue B into Queue A.
POP: Dequeue from Queue A
- 61
- 1
- 5
how to use a pair of Queues to create a Stack
Two regular (FIFO) Queues will be sufficient to implement a regular (LIFO) stack.
I'll show you the process with an example below:
The operation that should be done on our newly constructed Stack using two queues are:
Push A , B , C
Pop C
Push D
Lets start:
- Push A , B , C
Enqueue A , B , C to Queue 1
╔═════════╤═════════════╤═══╤══════╗
║ Queue 1 │ A │ B │ C ║
╠═════════╪═════════════╪═══╪══════╣
║ │ Head │ │ Tail ║
╟─────────┼─────────────┼───┼──────╢
║ Queue 2 │ - │ - │ - ║
╟─────────┼─────────────┼───┼──────╢
║ │ Head & Tail │ │ ║
╚═════════╧═════════════╧═══╧══════╝
- Pop C
Dequeue all element for Queue 1 expect tail element
Enqueue those elements (Dequeued from Queue 1) to Queue 2
Dequeue Tail from Queue 1
╔═════════╤══════╤══════╤═════════════╗
║ Queue 1 │ - │ - │ C ║
╠═════════╪══════╪══════╪═════════════╣
║ │ │ │ Head & Tail ║
╟─────────┼──────┼──────┼─────────────╢
║ Queue 2 │ A │ B │ ║
╟─────────┼──────┼──────┼─────────────╢
║ │ Head │ Tail │ ║
╚═════════╧══════╧══════╧═════════════╝
- Push D
Enqueue element to Queue 2
╔═════════╤═════════════╤═══╤══════╗
║ Queue 1 │ - │ - │ - ║
╠═════════╪═════════════╪═══╪══════╣
║ │ Head & Tail │ │ ║
╟─────────┼─────────────┼───┼──────╢
║ Queue 2 │ A │ B │ D ║
╟─────────┼─────────────┼───┼──────╢
║ │ Head │ │ Tail ║
╚═════════╧═════════════╧═══╧══════╝
PUSH :
Case 1 - empty stack
Enqueue into any Queue (1 or 2)
Case 2 - non empty stack
Enqueue into non empty Queue
POP :
Dequeue all element except tail of the non empty Queue (say Queue 1)
Enqueue all those element to the other empty Queue (say Queue 2)
Dequeue from Queue 1
how to use a pair of Stacks to create a Queue
Two regular (LIFO) stacks will be sufficient to implement a regular (LIFO) Queue.
Exmaple:
The operation that should be done on our newly constructed Queue using two stacks are:
Enqueue A , B , C
Dequeue C
Enqueue D
Lets start:
- Enqueue A , B , C
Push A , B , C to Stack 1
╔═════╤═════════╤═════╤═════════╗
║ │ Stack 1 │ │ Stack 2 ║
╠═════╪═════════╪═════╪═════════╣
║ Top │ C │ Top │ - ║
╟─────┼─────────┼─────┼─────────╢
║ │ B │ │ ║
╟─────┼─────────┼─────┼─────────╢
║ │ A │ │ ║
╚═════╧═════════╧═════╧═════════╝
- Dequeue C
Pop all elements from Stack 1
Push all these elements (popped from stack 1) to Stack 2
Pop from Stack 2
╔═════╤═════════╤═════╤═════════╗
║ │ Stack 1 │ │ Stack 2 ║
╠═════╪═════════╪═════╪═════════╣
║ Top │ - │ Top │ A ║
╟─────┼─────────┼─────┼─────────╢
║ │ │ │ B ║
╟─────┼─────────┼─────┼─────────╢
║ │ │ │ C ║
╚═════╧═════════╧═════╧═════════╝
- Enqueue D
Push D into Stack 2
╔═════╤═════════╤═════╤═════════╗
║ │ Stack 1 │ │ Stack 2 ║
╠═════╪═════════╪═════╪═════════╣
║ Top │ - │ Top │ D ║
╟─────┼─────────┼─────┼─────────╢
║ │ │ │ B ║
╟─────┼─────────┼─────┼─────────╢
║ │ │ │ C ║
╚═════╧═════════╧═════╧═════════╝
Enqueue:
Case 1 : Empty queue
Push into any stack (1 or 2)
Case 2 : Non empty queue
Push into non empty stack
Dequeue:
Pop all the elements from the non empty stack
Push all those elements (popped from non empty stack) to the empty stack
Pop top of non empty stack.
- 551
- 2
- 13
Two stacks to implements a queue -- always fill the stack A in stack B: the last element in stack A is popped (now you got FIFO) -- return elements from stack B to stack A.
Same idea is applied to queues.
- 2,432
- 19
- 25