Tutorial 10: Stacks and Queues

The Adapter Pattern


Introduction

Stacks and queues are examples of structures with special accesses.  They can be easily implemented by composing with one of the linear recursive structures studied so far.  This is called the "adapter pattern".  The adapter pattern is used to make one class look like another class with a different set of public methods.   It comes in two flavors:

This tutorial consists of the simple task of implementing the abstract Stack and Queue structures by adapting the Circular List structures.  The coding should be trivial.


Exercises

  1. Write the code for IStack, IQueue, CListStack, CListQueue, CheckEmpty, CheckFull specified in the diagram below (SQDoc).  CLList and ICLVisitor are defined in Circular List.   This is the object adapter pattern.

    stackQueue.png (17551 bytes)
  2. To ensure that only specific types of objects are stored in our stacks and queues, we can also use the object adapter pattern as shown in the diagram below (Type Specific).  Write the code for StackInt and QueueString.

    stackInt.png (8574 bytes)
  3. Below is a two-way stack/list adapter using multiple inheritance (Two Way).  Write the code for CListStack2Way.

    twoWay.png (15168 bytes)
D. X. Nguyen, Nov. 7, 1999.
dxnguyen@cs.rice.edu