1 / 13

The Stack Class

The Stack Class. Final Review Fall 2005 CS 101 Aaron Bloomfield. Motivation. Same as for Vectors We want an easy way to store elements in a object without having to worry about manipulating arrays. Properties of our Stack class. It needs to have an array to hold the values

rehan
Download Presentation

The Stack Class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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

  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;

  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

  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

  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

  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; }

  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]; }

  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; }

  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; }

  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] + ", ";

  12. Using our Stack public 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)); }

  13. Program Demo • Stack.java

More Related