Stack ADT - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Stack ADT

Description:

Popping from an empty stack is meaningless. Displaying the top of an empty stack is meaningless ... What if stack has a fixed size and is full? Interface ... – PowerPoint PPT presentation

Number of Views:89
Avg rating:3.0/5.0
Slides: 15
Provided by: bal1
Category:
Tags: adt | stack

less

Transcript and Presenter's Notes

Title: Stack ADT


1
Stack ADT
  • IC312
  • A Linear Data Type

2
Stack ADT
  • Stack might bring to mind something like this

What can we do to a stack like this?
3
Stack ADT Operations
  • Things we can do with a stack
  • Add entries to the top
  • Remove from the top
  • Display top entry
  • Display ALL entries
  • Get size of Stack
  • Find out if Stack is empty

4
Stack ADT Assumptions
  • Given the operations (behaviors) what are the
    assumptions/restrictions on the data?
  • All data is of same type
  • Data is in some specific order
  • First added at bottom, last at top
  • What assumptions have we made about data storage?
  • Relative order of data is maintained

5
Stack ADT
  • We can think of our list like this

Top
OR this
where the last thing we put on the top is the
first thing we pull out
Bottom
6
Specifications (modifiers)
  • Push (add)
  • Adds element to top of stack unless stack is full
  • Returns nothing
  • Pop (remove)
  • Removes element from top of stack unless stack is
    empty
  • Returns element from top

7
Specifications (accessers)
  • Top
  • Returns element from top of stack
  • getSize
  • Returns number of elements in stack
  • isEmpty
  • Returns true if stack is empty

8
Adding Entries
  • We always add to (push onto) top

Top
Bottom
9
Removing Entries
  • We always remove from (pop from) top

Top
Bottom
10
Different Situations
  • We must consider different possible situations
  • For Example
  • Is pushing onto an empty stack different than a
    populated stack?
  • What about popping?

11
Problem Situations
  • We must address potential problem situations
  • Popping from an empty stack is meaningless
  • Displaying the top of an empty stack is
    meaningless
  • What if stack has a fixed size and is full?

12
Interface
  • Defines methods a class must implement

interface StackltEgt bool push(E entry) E
pop() E top() int getSize() bool
isEmpty()
13
Implementation
  • Must implement all methods in interface
  • Need underlying data structure Something to
    hold the data
  • Can use array or list

14
Uses
  • Keeping track of jobs
  • In order to do x you first have to do y, but
    before that you have to do z
  • Keeping track of past events
  • undo and back
  • Post-fix expression evaluation
  • Checking for parentheses balance
Write a Comment
User Comments (0)
About PowerShow.com