Starting with Stacks

Our next unit in Computer Science is working with data structures. So far, all we have done is take keyboard input or hard-coded input and done immediate calculations with it. We’ve not actually stored or manipulated any data.

We’re going to start with stacks as those are arguably the easiest data structure to understand.

//Program Name: Starting with Stacks
//Programmer Name: Eric Evans, M.Ed.
//Programmer Organization: Ferris High School
//Program Date: Spring 2017

import java.io.*;
import java.util.*;

public class stacks {
    public static void main(String args[]){
        Stack myStack = new Stack();
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);
        myStack.push(4);
        myStack.push(5);
        myStack.push(6);
        myStack.push(7);
        myStack.push(8);
        myStack.push(9);
        myStack.push(0);
        System.out.println("Current Stack: " + myStack);
        System.out.println("Value of 1 is located " + myStack.search(1) + " positions from the top of the stack.");
        System.out.println("");
        System.out.println("First Removed Object is: " + myStack.pop());
        System.out.println("Current Stack: " + myStack);
        System.out.println("Value of 1 is located " + myStack.search(1) + " positions from the top of the stack.");
        System.out.println("");
        System.out.println("Next Removed Object is: " + myStack.pop());
        System.out.println("Current Stack: " + myStack);
        System.out.println("Value of 1 is located " + myStack.search(1) + " positions from the top of the stack.");
        System.out.println("");
        System.out.println("Next Removed Object is: " + myStack.pop());
        System.out.println("Current Stack: " + myStack);
        System.out.println("Value of 1 is located " + myStack.search(1) + " positions from the top of the stack.");
        System.out.println("");
        System.out.println("Next Removed Object is: " + myStack.pop());
        System.out.println("Current Stack: " + myStack);
        System.out.println("Value of 1 is located " + myStack.search(1) + " positions from the top of the stack.");
        System.out.println("");
        System.out.println("Next Removed Object is: " + myStack.pop());
        System.out.println("Current Stack: " + myStack);
        System.out.println("Value of 1 is located " + myStack.search(1) + " positions from the top of the stack.");
    }
}

As you can see here, we created a new stack on line 11 named myStack and then pushed the integer 1, 2, 3, 4, 5, 6, 7, 8, 9, & 0 into that stack on lines 12 through 21.

Line 22 displays the content of the stack, which at this moment is the following:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

Line 23 uses the search method to display the current position of a searched item, in this case, the integer “1”. Remember, the search method of a stack returns the position relative to the top of the stack. At this point in the application, the integer “1” is located at position 10.

At Line 25, we use the pop method to return the value at the top of the stack. Since this object was handled, it has now been removed from the stack.

Line 26 is identical to line 22 in that it displays the content of the stack at that moment. However, since the top item has been removed from the stack, it now looks like the following:

[1,2,3,4,5,6,7,8,9]

Line 27 is identical to line 23 and provides the position of the searched item, in this case the integer “1” in relation to the top of the stack. Since the top item has been removed, the search item is now one position closer to the top of the stack.

Lines 29-31, 33-35, 37-39, and 41-43 all perform the same functions as lines 25-27. Each time, the top item from the stack is removed, the current stack is displayed, and the position of the searched item in relation to the top of the stack is provided.

Interactive Stacks

Additionally, we will look at adding interactivity to the process as opposed to hard-coding the data.

//Program Name: Starting with Stacks
//Programmer Name: Eric Evans, M.Ed.
//Programmer Organization: Ferris High School
//Program Date: Spring 2017

import java.io.*;
import java.util.*;

public class stacks {
    public static void main(String args[]){
        Stack myStack = new Stack();
        Scanner myInput = new Scanner(System.in);
        System.out.print("Enter 10 Integers Separated by a Space and Press Enter: ");
        for(int myCounter=1; myCounter <11; myCounter++){
            myStack.push(myInput.nextInt());
        }
        System.out.println("Current Stack: " + myStack);
        System.out.println("Value of 1 is located " + myStack.search(1) + " positions from the top of the stack.");
        System.out.println("");
        System.out.println("First Removed Object is: " + myStack.pop());
        System.out.println("Current Stack: " + myStack);
        System.out.println("Value of 1 is located " + myStack.search(1) + " positions from the top of the stack.");
        System.out.println("");
        for(int myPopCounter=1; myPopCounter < 5; myPopCounter++){
            System.out.println("Next Removed Object is: " + myStack.pop());
            System.out.println("Current Stack: " + myStack);
            System.out.println("Value of 1 is located " + myStack.search(1) + " positions from the top of the stack.");
            System.out.println("");
        }
    }
}

Here, you can see that we’ve streamlined the code considerably. What was lines 25 through 43 is now lines 24 through 29! We utilized a for loop to execute the segment of code through 5 iterations.

In addition, the keyboard inputs are being processed through a for loop located at lines 14-16. This loop runs 10 iterations of the nextInt() method of the scanner object myInput and using the push method of the stack object myStack places them into the stack.

This code runs identical to the code above but the user can enter their own data as opposed to having to utilize the hard-coded data.

Items to Discuss

Next class, we’ll be discussing how the search handles multiple occurrences of the same item. For example, let’s say that a user entered their data to create the following stack:

[1,2,3,4,5,1,2,3,4,5]

In this case, if we used the search method to locate the integer “1”, which integer “1” would be displayed?

We will also analyze the advantages and the disadvantages of stacks as a data structure. This will take us to the next data structure, 1-dimensional arrays.

1 thought on “Starting with Stacks”

Leave a Reply

Your email address will not be published. Required fields are marked *