Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack using a queue

Tags:

algorithm

How could I implement a stack using a queue?

like image 858
Chander Shivdasani Avatar asked Oct 28 '10 01:10

Chander Shivdasani


1 Answers

push: insert the element into the back of the queue.

pop: remove an element from the front, insert it immediately in the back, repeat N-1 times, where N is the size of the queue, then remove the last element and return it.

like image 173
Eugene Smith Avatar answered Oct 18 '22 23:10

Eugene Smith