Two Stacks Make A Queue
November 8, 2013
In the previous exercise we used two queues to make a stack. In today’s exercise, we do the opposite: use two stacks to make a queue. This is exercise 10.1-6 from CLRS. In addition to solving the exercise, you should also create functions to implement a stack.
Show how to implement a queue using two stacks. Analyze the running time of the queue operations.
Your task is to write functions that use two stacks to implement a queue. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Implementation in Racket.
Haskell solution: