CS-151 Labs > Lab 5. Stack and Queue


Part 1. Stacks and Queues

We’ve been talking about stacks and queues in class, and now it is your time to put that theory to good use. Write two classes MyStack<T> and MyQueue<T> that implement the supplied interfaces StackADT and QueueADT, respectively.

Both MyStack and MyQueue can use a simple Linked List data structure to implement the corresponding Stack and Queue operations. In fact, we can reuse the MiniList.java implemented during the warmup. Make sure that your MiniList implements three methods: addFirst(), removeFirst() and addLast(). The method should be implemented in the most efficient way.

Now add an instance variable of type MiniList inside both MyStack and MyQueue.

For example, the definition of MyStack class could look like:

public class MyStack<T> implements StackADT<T> {
  private MiniList<T> list = new MiniList<T>();
}

MyStack implements StackADT interface. You can add all the unimplemented method stubs at once by pressing on red cross and selecting “Add unimplemented methods”.

MyStack
An implementation of the provided StackADT interface that is capable of storing an arbitrarily large amount of data. Use the private variable of type MiniList to implement all the required methods. Think which end of the list should be used for the most efficient pop/push operations.
MyQueue
An implementation of the provided QueueADT interface that is capable of storing an arbitrarily large amount of data. Use the same MiniList as an underlying data structure.

After implementing, you should add JUnit tests for MyStack and MyQueue that perform testing on your data structures. Call these MyStackTest and MyQueueTest.