STACKS - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

STACKS

Description:

STACKS READ CHAPTER 7 (P. 315 - 388) COP2532 STACKS Introduction stacks are a great way to understand the concept of providing a discipline over a data structure ... – PowerPoint PPT presentation

Number of Views:156
Avg rating:3.0/5.0
Slides: 13
Provided by: Jaso1164
Category:

less

Transcript and Presenter's Notes

Title: STACKS


1
STACKS
  • READ CHAPTER 7
  • (P. 315 - 388)

2
COP2532 STACKS
  • Introduction
  • stacks are a great way to understand the concept
    of providing a discipline over a data structure
  • only certain operations can have access to
    features of the stack
  • insertion and deletion from stack occurs from
    only one place
  • how does it work?
  • 1) values are added to or taken from the top of
    the stack
  • 2) adding an item is called pushing it onto the
    stack
  • 3) removing an item is called popping it off
    stack

3
Introduction to Stacks (cont.)
  • as mentioned before, a stack is how the system
    handles function calls in your program
  • last function called is first on stack
  • compiler keeps track of all variables for each
    call and uses stack to know where to return
  • as function ends, function is popped off stack
    and control moves to the next function on the
    stack
  • this procedure is called LIFO
  • should only use a stack if this type of structure
    is called for
  • if items need to be removed from other places
    other than the last entered, another structure
    should be used

4
Conceptual Design of a Stack
  • When a stack is created, it starts off empty (no
    items)
  • There is a position pointer that tells you where
    the current top of the stack is
  • To add a value to the stack, you push it on the
    stack
  • Remove values using pop
  • Need to ensure that the following two things are
    not done
  • 1) Attempting to push an item when the stack is
    full (called stack overflow)
  • 2) Attempting to pop an item when the stack is
    empty (called stack underflow)

5
STACK DESIGN (cont)
  • so, if we design the Stack as an object
  • Stack Object
  • Knows
  • Its items
  • Top position of stack (position pointer)
  • Can Do
  • Push an item on stack
  • Pop an item from stack
  • Tell how many items are currently on stack
  • Tell if stack is empty
  • Tell if stack is full
  • Tell you which item is currently sitting on top
    (without removing it)

6
Stack Implementation
  • typedef int Item
  • typedef Item ItemPtr
  • class Stack
  • public
  • static const int DEFAULTSIZE 30
  • Stack()
  • Stack(const int StackSize)
  • Stack()
  • Item Top()
  • Item Pop()
  • void Push(const Item anItem)
  • int Size() const
  • bool IsEmpty() const
  • bool IsFull() const
  • private
  • ItemPtr MyStack
  • int TopofStack_

7
The Member Functions
  • Stack Stack()
  • MyStack new ItemDEFAULTSIZE
  • TopofStack -1
  • Stack Stack(const int StackSize)
  • if ((StackSize lt 1) (StackSize gt 30))
  • StackSize DEFAULTSIZE
  • MyStack new ItemStackSize
  • TopofStack -1

8
MORE MEMBER FUNCTIONS
  • The Deconstructor - simply deletes the stack
    from the heap
  • Stack Stack()
  • delete MyStack
  • The IsEmpty Function - will return true if the
    stack is empty
  • bool Stack IsEmpty() const
  • return (TopofStack -1)
  • The IsFull Function - will return true if the
    stack is Full
  • bool Stack IsFull() const
  • return((TopofStack1) gt DEFAULTSIZE)

9
MORE MEMBER FUNCTIONS
  • The Size Function - will return the number of
    items currently on stack
  • int Stack Size() const
  • return(TopofStack 1)
  • The Push Function - takes an item as input and
    adds it to the stack
  • void Stack Push(const Item anItem)
  • if (IsFull())
  • cout ltlt "Cannot add - stack is
    full" ltlt endl
  • else
  • MyStackTopofStack
    anItem

10
MORE MEMBER FUNCTIONS
  • The Pop Function - returns the top item off the
    stack and resets TopOfStack
  • Item Stack Pop()
  • if (IsEmpty()) cout ltlt "Cannot pop -
    stack empty" ltlt endl
  • else
  • return(MyStackTopofStack--)
  • return -999
  • The Top Function - gives back the top item on
    stack but does not remove it
  • Item Stack Top() const
  • if (IsEmpty())
  • cout ltlt "No items on stack " ltlt endl
  • else
  • return(MyStackTopofStack
  • return -999

11
AN EXAMPLE OF USING THIS STACK - REVERSING DIGITS
  • void main()
  • Stack TheStack
  • char InputChar '0' //dummy digit to force
    loop to run
  • cout ltlt "Enter digits to display in reverse
    "
  • while (isdigit(InputChar))
  • cin.get(InputChar)
  • if (isdigit(InputChar))
  • TheStack.Push(InputChar)
  • char OutputChar
  • while (!TheStack.IsEmpty())
  • OutputChar TheStack.Pop()
  • cout ltlt OutputChar ltlt " "
  • cout ltlt endl

12
QUESTIONS?
  • Read section on LINKED STACKS
  • (P. 353 - 366)
Write a Comment
User Comments (0)
About PowerShow.com