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.
- Create files called DAStack.h, DAStack.cpp. The files
DAStackExceptions, DAStackTester.cpp, and makefile will be provided
- In your .h:
- use include directives and conditional compilation as
needed, don't forget your using statement.
- define constants: NULLVAL having value -1, and DEFAULT
having value 5
- define the type Element
to rename an int, and type ElementsInStack to
rename a pointer
to an Element.
- define a class DAStack. DAStack should include the
following:
- data members capacity,
top, and size of type int, and elementStack of type ElementsStack.
- default constructor
- copy constructor
- destructor (deletes entire dynamic array)
- overloaded assignment operator
- isEmpty predicate
function
- push mutator
function: takes in element of type Element
- pop mutator
function: throws Empty
exception
- getSize accessor
function: returns an int
- getTop accessor
function: returns an Element
- display (takes in ostream, displays all elements in the stack)
- read (takes in
istream. Unlike the Bag read, this version reads in any number of element and pushes it on a stack)
- depending on how you design your class, you may also have
helper (member) functions such as modifyArraySize
&/or verifyAllocation (Do not repeat code unnecessarily if you can write a function instead. Try to reuse your code as much as possible)
- provide prototypes for non-member overloaded output and input operator functions
- In your .cpp:
- use include directives as needed, don't forget your using
statement.
- define the following member functions (don't forget to use your
class name and scope operator);
- default constructor: create dynamic array of Element of
size DEFAULT pointed to by elements, initialize capacity to DEFAULT,
top to NULLVAL, and size to (top+1). Use an initializer list wherever possible.
- copy constructor: given an instance of DAStack,
instantiate a new DAStack object whose data members have exactly the
same values as the given object. Use an initializer list wherever possible.
- destructor: delete the dynamic array
- overloaded assignment operator: as usual
- isEmpty:
return TRUE if top is equal
to NULLVAL
- push: grow the
array if necessary, increment top,
increment size, insert element into the array element
pointed to by top.
- pop: throw Empty exception (from DAStackExceptions.h) if stack empty;
otherwise,
remove the top element,
decrement top, decrement size, shrink the array
if necessary.
- getSize:
return size.
- getTop: throw Empty exception if stack empty;
otherwise, return a copy of the top element
- display:
output the elements in the stack from the top down.
- read: input a
bunch of elements and push them onto the
stack.
- modifyArraySize: creates new larger or smaller
array, copies elements from old array to new array, then switches
pointer.
- verify allocation
(optional): checks to see if dynamic
array allocation was successful.
- define the overloaded input and output operator non-member
functions (as usual).
- Provided file DAStackExceptions.h:
- defines class Empty
with message data member of
type string, explicit value constructor that takes in a message
of type string, and getMessage accessor function that
returns message.
- Provided file DAStackTester.cpp:
- Excercises your class. You can view your output in file Stack.txt (open it in Wordpad if necessary).
- Provided file makefile:
- Compiles, links, and runs your program. Places output in Stack.txt
- Whenever you want to clean up, invoke: make clean
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).
- Create files called PStack.h and PStack.cpp. PStackExceptions, PStackTester.cpp, and makefile will be provided for you.
- In your .h:
- use include directives and conditional compilation as
needed, don't forget your using statement.
- define constants: NULLVAL having value 0, and INIT having
value 0
- define the type Element
to rename an int.
- define a class PStack.
PStack should include
the
following:
- nested Node struct or class having data members data of type element
and next of type pointer to Node;
having a default constructor that
sets data to INIT and next to NULLVAL, one explicit value
constructor
that sets data to the
incoming parameter and next
to NULLVAL, and one
explicit value constructor that sets both data and next to incoming
parameters.
- define the type Link
to rename a pointer to a
Node
- data members top
of type Link and size of type int
- default constructor
- copy constructor
- destructor
- overloaded assignment operator
- isEmpty predicate
function
- push mutator
function: takes in element of type Element
- pop mutator
function: throws Empty exception (found in file PStackExceptions.h)
- getSize accessor
function: returns an int
- getTop accessor
function: returns an Element
- display (takes in ostream, displays elements in stack from top down)
- read (takes in
istream, inputs one or more elements and pushes them on the stack)
- provide prototypes for non-member overloaded output and input operator functions
- In your .cpp:
- use include directives as needed, don't forget your using
statement.
- define the following member functions (don't forget to use your
class name and scope operator);
- default constructor: set top to
NULLVAL, and size to
INIT.
- copy constructor: given an instance of DAStack,
instantiate
a new DAStack object whose data members have exactly the same values as
the given object.
- destructor: delete each node in the stack
- overloaded assignment operator: remember to destroy the old nodes and allocate
the new nodes one at a time
- isEmpty:
return TRUE if top is equal
to NULLVAL
- push: allocate
a new node, set its data
field, connect
its next field to the current
top, set top to the new node, and
increment size.
- pop: throw Empty exception (from file PStackExceptions.h) if stack empty;
otherwise,
set top to point to the node
pointed to by top->next,
delete the
previous top node, and
decrement size.
- getSize:
return size.
- getTop: throw Empty exception if stack empty;
otherwise, return a copy of the top
element
- display:
output the elements in the stack from the top down.
- read: input a
bunch of elements and push
them onto the
stack.
- define the overloaded input and output operator non-member
functions (as usual).
- Note:
there is no insert function, nor is there a remove function. Not
only is the logic easier and more straightforward, you also don't have
any situations where you have to qualify the Link type since it won't
appear as a return type here.
- Provided file PStackExceptions.h:
- defines class Empty
with message data member of
type string,
explicit value constructor that takes in a message of type string,
and getMessage accessor
function that returns message.
- Provided file PStackTester.cpp:
- Excercises your class. You can view your output in file Stack.txt (open it in Wordpad if necessary).
- Provided file makefile:
- Compiles, links, and runs your program. Places output in Stack.txt
- Whenever you want to clean up, invoke: make clean
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