CS 231: Data Structures and Algorithms (Lab Page)

Title image Lab 3
Fall 2016

Stacks

The goal of this lab period is to give you an opportunity to implement a stack.

Tasks

  • Make a new folder for this week's project.

    Implement an IntStack class, which usses an array of ints to store the elements. It should have a field (of type int) to keep track of the index of the top of the stack and a field to store the elements (int[]). It should implement the following methods:

    1. public IntStack( ) - constructor method. Make an array of 10 ints and initialize the top index to be 0.
    2. public void push( int new_item ). Push a new item onto the stack. If there isn't enough room in the array, then make a new array and copy the data over.
    3. public int pop(). Pop and item off the stack and return it.
    4. public int elementAt( int index ). Return the element at the given position (do not modify the stack).
    5. public int size(). Return the number of items in the stack.
    6. public boolean isEmpty(). Returns true if the stack is empty.
    7. Write a main method testing every method in your class. The test it and debug it until it is perfect. :)

      One of your tests should be to push 20 items onto your stack.

    Once you have completed the lab and feel comfortable with stacks, go on to the project.