Algorithms Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Explain how a new element can be added to a queue using two stacks.

A

-(Two stacks are required) so that the second stack can reverse the order of the first stack.
- Stack 1 operates as the queue with the newest elements at the bottom. Stack 2 is empty.
- To add an element, pop all the elements from stack 1 and push onto stack 2.
-Push the new element onto either stack.
-Pop all the elements of stack 2 back onto stack 1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly