Homework 17


Due : 9.30am, 4 August

Write code to solve the following problems using the ADTs for stacks and queues as discussed in class. In both instances, S is a stack and Q is a queue.

  1. Suppose S is empty and Q is not. Use S to reverse the contents of Q.
  2. Suppose S is not empty and Q is. Use Q to reverse the contents of S.

Solution

  1. while (!Q.Empty ())
       S.Push (Q.DeQue ());
    while (!S.Empty ())
       Q.EnQue (S.Pop ());
    
  2. while (!S.Empty ())
       Q.EnQue (S.Pop ());
    while (!Q.Empty ())
       S.Push (Q.DeQue ());
    

Last updated : 7 August 2000 8.30pm