13
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield

1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield

Embed Size (px)

Citation preview

Page 1: 1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield

1

The Stack Class

Final ReviewFall 2005CS 101Aaron Bloomfield

Page 2: 1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield

2

Motivation Same as for Vectors

We want an easy way to store elements in a object without having to worry about manipulating arrays

Page 3: 1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield

3

Properties of our Stack class It needs to have an array to hold the values

That array will be fixed at size 1000 We’ll call it ‘array’

Thus, we also need a value to hold the number of elements in the stack We’ll call it ‘size’

Our Stack class so far:

public class Stack {

int array[] = new int[1000]; int size = 0;

Page 4: 1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield

4

About stacks With a stack, you can only add and remove elements from

ONE end Both occur from the same end

Think of a stack of papers – you are always adding a new paper to the top or removing it from the top

Adding an element is called pushing an element Removing an element is called popping an element

Page 5: 1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield

5

Methods in our Stack class Create a Stack object Insert and remove elements into/from the Stack Get the top element Find if the stack is empty Print it out to the screen Search the stack for a value

Page 6: 1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield

6

The Stack Constructor Ready?

public Stack () {}

Rather boring… As we initialized the variables earlier, we don’t need to do so

here But we could have done here just as well

Page 7: 1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield

7

Pushing an element onto the Stack To add an element onto the Stack, we want to do two things

Insert it into the array at the proper position Increment the size of the array

The code:

public void push (int value) {array[size++] = value;

}

We could have done this in two lines as well:

public void push (int value) {array[size] = value;size = size + 1;

}

Page 8: 1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield

8

Popping an element from the Stack To add an element onto the Stack, we want to do two things

Find (and return) the top element in the Stack Decrement the size of the array

The code:

public int pop () {return array[--size];

}

We could have done this in two lines as well:

public int pop () {size = size – 1;

return array[size];}

Page 9: 1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield

9

peek() and empty() peek() is a quickie:

public int peek () {return array[size-1];

}

empty() is also a quickie:

public boolean empty() {return size == 0;

}

Page 10: 1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield

10

Searching the Stack Note that we want to search up to the value in size, not the

entire (1,000 element) array

The method:

public boolean search (int forwhat) {for ( int i = 0; i < size; i++ ) if ( array[i] == size )

return true;return false;

}

Page 11: 1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield

11

Printing the Stack The method:

public String toString() {String ret = "Stack[";for ( int i = 0; i < size; i++ ) { ret += array[i]; if ( i != size-1 )

ret += ", ";}ret += "]";return ret;

}

This is just so that the method doesn’t print a comma after the last element

Our for loop body could also have been:ret += array[i] + ",

";

Page 12: 1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield

12

Using our Stackpublic static void main (String args[]) {

Stack stack = new Stack();

System.out.println ("pushing elements 5 through 10 onto the stack");for ( int i = 5; i <= 10; i++ ) stack.push(i);

System.out.println (stack);System.out.println ("peek() returned: " + stack.peek());

System.out.println ("search(9) returned: " + stack.search(9));

int ret = stack.pop();System.out.println ("pop() returned: " + ret);

System.out.println (stack);System.out.println ("peek() returned: " + stack.peek());

ret = stack.pop();System.out.println ("pop() returned: " + ret);ret = stack.pop();System.out.println ("pop() returned: " + ret);

System.out.println (stack);System.out.println ("peek() returned: " + stack.peek());

System.out.println ("search(9) returned: " + stack.search(9));}

Page 13: 1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield

1313

Program DemoProgram Demo

Stack.javaStack.java