The goal of this assignment is to practice using Vectors and linked lists.
As usual, make a new directory for this project and cd into it.
A stack is a container for data (a data structure) characterized by the ability to add elements and then retrieve them in the opposite order from which they were added. We have seen one example of this---the structure the virtual machine uses to keep track of what methods are currently invoked.
For a real-world analogy, think of a PEZ dispenser. Suppose you added to an empty dispenser a red piece, a green piece, and a blue piece, one at a time and in that order. You would not then be able to get at the red piece directly. Instead, if you opened the head, you could retrieve the blue piece, and then the green and red, in that order. The order in which you remove the pieces is the reverse of the order in which you added them. This is called last-in/first-out (LIFO) order.
More formally, a stack is a data structure that has the following operations:
In addition, we will implement the following extra operations:
Consider this illustration of executing a push and two pops on a stack of characters that already has three elements in it:
Note that a set of operations essentially defines an interface. There is more than one way to implement a stack, as we will see in this assignment.
Note also that the way you interact with a stack is fundamentally different from how you interact with an array (although we can use an array to implement a stack). In an array, you can read from or write to any position at any time (this is called random access). In a stack, you can interact only with the end of the stack, regardless of what position the end is.
Copy the following files from the class directory:
> cp /homeemp/tvandrun/pub/235/Stack.java . > cp /homeemp/tvandrun/pub/235/StackDriver.java . > cp /homeemp/tvandrun/pub/235/ArrayStack.java .
Stack.java
contains an interface for stack-like classes.
We will assume that the data we will work on is of type char
;
ie, we'll use stacks of characters.
StackDriver.java
contains a driver to test various
implementations of Stack.java
.
It uses the stacks for a classic stack application: reversing
strings.
Familiarize yourself with these.
Note that StackDriver.java
chops strings up into
one-character-long String
s rather than
char
s.
This will make using a Vector
to implement
a stack easier.
ArrayStack.java
defines a class that implements
a stack using an array.
It is already finished for you.
Your first task is to inspect this code carefully and understand how it works.
Note that the instance variable top
always points to the
next available slot.
Note also that position 0 is always the bottom of the stack,
and the stack grows "up" (more like a stack of books on a table
which gets higher the more books you put on it than
like a PEZ dispenser that grows "down" as the spring compresses).
We have already seen how String
s can be used
as containers for char
s.
Write a class StringStack
which implements
Stack
but uses a String
as its underlying
data structure.
Some things to note: make use of string contatenation, substring
and charAt
.
It is possible to do this without a top
instance variable.
Also, the stack need not ever be full---because we can keep on contatenating
things to the string.
Follow the example of linked lists from class, and implement
a stack using a linked list.
Take note--- if you understand how linked lists work this
is extremely easy, and most of these methods are one-liners;
the difficult part is understanding how linked lists work.
depth()
and multiPop
can be written recursively,
so write two classes, IterativeListStack
and RecursiveListStack
, computing
depth()
and multiPop
iteratively and recursively,
respectively.
(depth()
and multiPop
do not have to be
technically recursive themselves; you may find it useful to
have them call recursive helper methods).
You will need a Node
class; you may
choose to have two such classes, INode
for IterativeListStack
ande RNode
for RecursiveListStack
.
Write a class VectorStack.java
so that it
defines a class that works just
like the other two, but uses a Vector
.
Copy the files you modified to
/homestu/jmichalk/labTA/turnInProjects/
yourFirstName,yourLastInitial
by 5:00 thursday, Nov 16