This lab will help you learn about linked lists. You will also become familiar with packaging, classification, and polymorphism in Java and Object Oriented Design in general. Based on the class hierarchy built for Queue you will build a the components of the class hierarchy for Stack.
cd public-html/classes/csc241
cp -r ~mohammad/public-html/classes/csc241/Queue .
-- don't forget the dot or the -r. This command replicates my Queue directory and places it in your account.
cd Queue
javac *.java
will
compile all Java files in that directory. So, in the
Queue directories compile the .java files.
java test test.dat dynamic
java test test.dat fixed 10
You need to create the Stack directory and the three files Stack.java, fixedStack.java, and dynamicStack.java and compile them. These files need to be packaged as csc241.Stack. You will also need to create testStack directory as it exists for the Queue package as well and test to make sure your Stack classes work correctly.
Stack.java will hold the interface for stack and follows closely the
Queue interface.
fixedStack.java implements the Stack interface. You should follow the code we developed in class for Stack of Objects. You will need a peek method as the interface calls for it; it didn't exist in our class example. Also, keep in mind that push and pop's return value types need to be void.
dynamicStack.java also implements the Stack interface. Following dynamicQueue works only partially.
You will need a Snode class which will be basically the same as the Qnode class used for dynamicQueue.
You will only need one field here, a top_ which references the top node in our linked list. top_ should be null at construction.
The methods empty, full, and peek are similar to their couterparts in dynamicQueue.
The pop should be similar to the dequeue method from dynamicQueue as the concept of removing from the top of the stack is really the same as removing from the front of the queue. We don't have a back of the list concept in the stack so there is no back_ reference that may have to be set to null, you only need to deal with top_.
The push method, on the other hand, is different from enqueue as it needs to put the new element on top, not in the back. To push an element, you simply put the element in element_ of a new node and have its next_ reference the current node on top( i.e. the node referenced by top_); top_ would then refernce this new node.
test.java will be just like the test program for the queue class, except for the fact that it will be testing a stack; it needs stack's constructors and methods. Make sure that both implementations work before turning the lab in.