Stacks - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Stacks

Description:

peek(): Return the top element of the stack. push(x): Add ... Object peek () Looks at the object at the top of this stack without removing it from the stack. ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 24
Provided by: richardu
Category:
Tags: peek | stacks

less

Transcript and Presenter's Notes

Title: Stacks


1
Stacks
2
Overview
  • To frame our discussion, consider

3
Outline
4
Stack (1)
  • Note in lists the order in which items are placed
    in the list was not considered important. There
    are situations where this information must be
    maintained.

5
Stack (2)
  • A stack represents a collection in which elements
    are maintained in the order in which they are
    inserted. At any moment, only one element of the
    stack may be removed, and that is the element
    that has been on the stack the shortest time.

6
Stack (3)
  • A stack is a specialized list. We achieve the
    specialization by restricting adds and removes to
    one end of the collection.

7
Stack (4)
  • Stacks are referred to as LIFO structures
    Last-In-First-Out

8
Example
2
9
9
3
3
7
7
5
5
9
Stacks?
  • Remember recursion?
  • We spoke of the run-time stack.
  • Why is this model appropriate for program
    execution?
  • Remember every time you issue a method call you
    create an activation record. Order is important!

10
ADT Stack
  • instances
  • linear list of elements
  • one end of the stack if referred to as the top.
  • operations
  • empty() Return true if the stack is empty
  • peek() Return the top element of the stack.
  • push(x) Add element x to the top of the stack.
  • pop() Remove the top element of the stack.

11
Example
9
order of execution
3
s.push(5) s.push(7) s.push(3) s.push(9)
7
5
12
Example
s.peek()
9
3
7
5
13
Example
s.pop()
9
3
7
5
14
Example
s.push(1)
9
1
3
3
7
7
5
5
15
Collection Framework
  • java.lang.Object
  • java.util.AbstractCollection
  • java.util.AbstractList
  • java.util.Vector
  • java.util.Stack
  • All Implemented Interfaces Cloneable ,Collection
    ,List ,RandomAccess ,Serializable

16
  • public class Stack extends Vector
  • Object peek ()           
  • Looks at the object at the top of this stack
    without removing it from the stack.  
  • Object pop ()           
  • Removes the object at the top of this stack and
    returns that object as the value of this
    function.  
  • Object push (Object  item)           
  • Pushes an item onto the top of this stack.  
  • int search (Object  o)           
  • Returns the 1-based position where an object is
    on this stack.

17
Constructor
  • // Postcondition this Stack object is empty.
  • public Stack ()
  • Creates an empty Stack.

18
  • // Postcondition the number of elements in this
  • // Stack object has been returned.
  • public int size( )

19
  • // Postcondition true has been returned if this
    Stack
  • // object has no elements. Otherwise,
  • // false has been returned.
  • public boolean isEmpty( )

20
  • // Postcondition element has been inserted at
    the top of
  • // this Stack object and
    returned. The
  • // averageTime (n) is
    constant. The
  • // worstTime (n) is O
    (n), but for n
  • // consecutive calls to push,
    worstTime(n)
  • // for the entire sequence still only O (n).
  • public Object push (Object element)

21
  • // Precondition this Stack object is not empty.
  • // Otherwise, an exception will be thrown.
  • // Postcondition the element that had been at
    the top of this
  • // Stack object before this method was called
  • // has been removed and returned.
  • public Object pop( )

22
  • // Precondition this Stack object is not empty.
  • // Otherwise, an exception will be thrown.
  • // Postcondition (a reference to) the top
    element of
  • // Stack object has been returned.
  • public Object peek( )

23
Issues
  • Performance
  • Enforcement
Write a Comment
User Comments (0)
About PowerShow.com