Stacks - PowerPoint PPT Presentation

About This Presentation
Title:

Stacks

Description:

As items are added they are chronologically ordered, items are removed in ... To avoid always check isEmpty before popping. Normal methods ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 11
Provided by: stef91
Category:
Tags: popping | stacks

less

Transcript and Presenter's Notes

Title: Stacks


1
Stacks
  • CS-240
  • Dick Steflik

2
Stacks
  • Last In, First Out operation - LIFO
  • As items are added they are chronologically
    ordered, items are removed in reverse
    chronological order (newest first)

3
Applications
  • reversing the items in a list
  • returning from a function/subroutine
  • evaluation of postfix expressions
  • converting infix expressions to postfix
  • localization of parameters and variables in a
    function

4
Overflow and Underflow
  • Stack overflow is the condition that arises when
    you attempt adding (pushing) an item onto an
    already full stack.
  • To avoid always check isFull( ) before adding
  • Stack Underflow is the condition that arises when
    you attempt to remove (pop) an item from an
    already empty stack
  • To avoid always check isEmpty before popping

5
Normal methods
  • constructor - create and initialize a stack
    object
  • copy constructor - create a stack object that is
    a duplicate of another existing stack object
    (this method needed to insure correct value
    semantics)
  • overloaded assignment operator - assign the value
    of an existing stack object (a) to another
    existing stack object (b) so that the result is
    that b is a duplicate of a (this method needed to
    insure correct value semantics)
  • destructor - destroy a stack object by returning
    any of its dynamic storage back to the heap and
    setting its static elements to NULL or zero

6
Methods
  • push - add an item (as the most recent)
  • pop - delete the most recently added item
  • pull - return the value of the most recently
    added item then delete the item
  • peek - return the value of the most recently
    added item
  • ifFull - return false/true depending if the stack
    is full true if it is, false otherwise
  • isEmpty - return false/true depending if the
    stack is empty true if it is, false otherwise

7
Private data strategies
  • use an array to hold items and use an int as an
    index for the array to indicate where the next
    item is to go
  • same as above, but use a dynamic array
  • use a struct to define a node and add nodes
    dynamically as they are needed use one static
    pointer to a node at point at most recently added
    item(top of stack)

8
Static Static Implementation
Class Stack public Stack( )
top 0 ) void push(int v)
datatop v void pop( )
--top int pull( ) return
datatop-- int peek( ) return
datatop ) boolean isFull( )
return top 20 boolean isEmpty(
) return top 0 private
int data20 int top
9
Dynamic Array Implementation
Class Stack public Stack( )
size 20 top 0 data new int20
Stack( int s ) size s top 0
data new ints ) void push(int
v) if (this.isFull( )) temp new
intsize2 size size 2

for (int j 0 j lt top j)

tempj dataj

delete data data temp
datatop v
int pop( ) return data--top
boolean isFull( ) return top size
boolean isEmpty( ) return top 0
private int data
int top int size
10
Dynamic (nodal) implementation
Class Stack public Stack( )
top NULL ) Stack( ) while
(!this.isEmpty()) this.pop( )
void push(int v) node temp new node()
temp -gt data v
temp -gtnext top top temp
int pull( ) int value top-gtdata
node temp top-gtnext delete top top
temp void pop( ) node temp
top-gtnext delete top top temp
boolean isEmpty( ) return top NULL
private struct node int data
node next
node top
Write a Comment
User Comments (0)
About PowerShow.com