The Stack Class - PowerPoint PPT Presentation

About This Presentation
Title:

The Stack Class

Description:

peek() and empty() peek() is a quickie: public int peek () { return array[size-1] ... System.out.println ('peek() returned: ' stack.peek ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 14
Provided by: jimco60
Category:
Tags: class | peek | stack

less

Transcript and Presenter's Notes

Title: The Stack Class


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
  • Well call it array
  • Thus, we also need a value to hold the number of
    elements in the stack
  • Well call it size
  • Our Stack class so far
  • public class Stack
  • int array new int1000
  • 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 dont
    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)
  • arraysize value
  • We could have done this in two lines as well
  • public void push (int value)
  • arraysize 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 arraysize

9
peek() and empty()
  • peek() is a quickie
  • public int peek ()
  • return arraysize-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 lt size i )
  • if ( arrayi size )
  • return true
  • return false

11
Printing the Stack
  • The method
  • public String toString()
  • String ret "Stack"
  • for ( int i 0 i lt size i )
  • ret arrayi
  • if ( i ! size-1 )
  • ret ", "
  • ret ""
  • return ret
  • This is just so that the method doesnt print a
    comma after the last element
  • Our for loop body could also have been
  • ret arrayi ", "

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 lt 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())

13
Program Demo
  • Stack.java
Write a Comment
User Comments (0)
About PowerShow.com