CS-240
Lab 6

Purpose:

Introduction to stacks

Part I:  Stacks

A stack is one of the most frequently used data structures, and (fortunately!) one of the simplest.  A stack is basically a more restricted form of a linear list, i.e., one in which  insertions and deletions are limited to one end only.  Another description of a stack is a structure that handles data in a last-in-first-out (LIFO) manner.  For example, if you were to load plates into a cupboard one by one, the last plate that you stacked (i.e., the top one) would be the first plate that you would pull off (or pop) the next time you opened the cupboard to get a plate.  The same goes for stacks of books, magazines, cards, etc.  In other words, the stack data structure behaves in exactly the same manner as real world, every day kinds of stacks that you're familiar with.  In fact, you have already managed a stack with a dynamic array when you implemented the Bag class, and you managed it as an array-based linked stack when you managed the free store in your array-based linked lista simulated linked list (note that you always allocated a node from one end of the free list, and deallocated a node by returning it to the same end of the free list).  Because you can insert and delete only from one end, operations such as searching for an item, finding the correct insertion/deletion point, etc. are completely unecessary (that's why it's simpler!).

The only required data member of a stack ADT is top (although implementations of this ADT may include other members such as size or capacity).
The basic operations associated with the stack ADT are:  construct an empty stack, check to see if the stack is empty, push (add an element to the top of the stack), top (or getTop:  read the element at the top of the stack), and pop (remove the element at the top of the stack).  Implementations of a stack can also include read  and overloaded >> (read in an item and push it onto the stack), and display and overloaded << (display all the items in the stack from top down.  Note that this is "cheating":  when we display all the items in the stack we ignore the "access the top only" stack constraint.

A.  Dynamic Array Implementation:

A stack can be easily implemented with a static or dynamic array.  The basic methodology for managing a stack implemented in this way  is as follows:
 Have a variable top point to the top of the stack (i.e., contain the subscript of the dynamic array element at the top of the stack).  top is initialized to null to start.  Whenever an element is put on the stack, top is first incremented to point to the next array element, and the value of the element is stored in the new top position.  Whenever an element is popped off the stack, top is decremented to point to the array element below the current top, thus becoming the new top.
As you follow the instructions below,  you can choose to develop your stack program from scratch, or you can start with your dynamic array implementation of a Bag, rename it, and modify it accordingly.
Note:  this is NOT a linked structure, there are no nodes, there is no next field, and there are no Links.  However, we are managing dynamically allocated memory.
  1. Create files called DAStack.h,  DAStack.cpp.  The files DAStackExceptions, DAStackTester.cpp, and makefile will be provided
  2. In your .h:
  1. In your .cpp:
  1. Provided file DAStackExceptions.h:
  2. Provided file DAStackTester.cpp:
  3. Provided file makefile:

B.  Pointer-Based Implementation:

A pointer-based implementation of a stack is similar to (but simpler than)  that of an ordinary linked list.  As you follow the instructions below,  you can choose to develop your stack program from scratch, or you can start with your pointer-based version of LinkedList, rename it, and modify it accordingly.
Note:  this IS a linked structure, all the elements are wrapped in nodes with next field (i.e., Link). 
  1. Create files called PStack.h and PStack.cpp.  PStackExceptions, PStackTester.cpp, and makefile will be provided for you.
  2. In your .h:
  1. In your .cpp:
  1. Provided file PStackExceptions.h:
  2. Provided file PStackTester.cpp:
  3. Provided file makefile:
Submission:  FTP all files to your FTP account and put your hard copies in the cabinet as usual, by March 12 (Monday) at midnight

Last updated:  3/3/07 - RMW