LINKED STACKS - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

LINKED STACKS

Description:

a way around that is to combine the concept of a linked list and a stack to ... The Pop Function - returns the top item off the stack and resets top to point to ... – PowerPoint PPT presentation

Number of Views:93
Avg rating:3.0/5.0
Slides: 12
Provided by: jasont6
Category:
Tags: linked | stacks | pop | question | the | to | ways

less

Transcript and Presenter's Notes

Title: LINKED STACKS


1
LINKED STACKS
  • READ P. 353 - 366

2
LINKED STACKS
  • Introduction
  • again, there is a limitation to lists that was
    discussed earlier (the fixed size of the array
    limits the list)
  • in our stack example, we are still limited to
    using only the size specified by the constructor
    for our stack
  • a way around that is to combine the concept of a
    linked list and a stack to produce a linked stack
    list
  • Implement stack as linked list, and expand or
    shrink the stack through pop and push

3
Conceptual Design of Linked Stack
  • again, items are only added/removed from one
    place (we'll call it top)
  • for the linked stack, only the top node will be
    available, and we'll traverse through the stack
    by following links
  • rest of the commands are still the same
  • Linked Stack Object
  • Knows 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

4
Implementation of the Linked Stack
  • typedef int Item
  • struct Node
  • Item data
  • Node link
  • class LinkedStack
  • public
  • LinkedStack()
  • Item Top()
  • Item Pop()
  • void Push(const Item anItem)
  • int Size() const
  • bool IsEmpty() const
  • bool IsFull() const
  • private
  • Node top //pointer to top of stack

5
Linked Stack Member Functions
  • The Default Constructor - creates a stack
    dynamically, setting top to point to nothing
  • LinkedStack LinkedStack()
  • top 0
  • The IsEmpty Function - will return true if the
    stack is empty (top points to nothing)
  • bool LinkedStack IsEmpty() const
  • if (top 0)
  • return true
  • else
  • return false

6
Linked Stack Member Functions (cont.)
  • The IsFull Function - will return false always
    (dynamic memory being used)
  • bool LinkedStack IsFull() const
  • return(false)
  • The Size Function - moves through the items by
    traversing links , counting as we go
  • int LinkedStack Size() const
  • Node temp int total 0
  • temp top
  • while (temp ! 0)
  • total
  • temp temp-gtlink
  • return total

7
Linked Stack Member Functions (cont.)
  • The Push Function - moves to find the current top
    of stack, and adds the item there
  • void LinkedStack Push(const Item anItem)
  • Node temp temp new Node
  • if (temp 0)
  • cout ltlt "out of memory"
  • else
  • temp-gtdata anItem
  • temp-gtlink top
  • top temp

8
LINKED STACK MEMBER FUNCTIONS (cont.)
  • The Pop Function - returns the top item off the
    stack and resets top to point to next item
  • Item LinkedStack Pop()
  • Item info Node temp
  • if (IsEmpty())
  • cout ltlt "Cannot pop - stack empty"
    ltlt endl
  • return -999
  • else
  • temp top
  • info temp-gtdata
  • top temp-gtlink
  • delete temp
  • return info

9
LINKED STACK MEMBER FUNCTIONS (cont.)
  • The Top Function - gives back the top item on
    stack but does not remove it
  • Item LinkedStack Top() const
  • Item info Node temp
  • if (IsEmpty())
  • cout ltlt "No items on stack "
    ltlt endl
  • return -999
  • else
  • temp top
  • info temp-gtdata
  • return info

10
EXAMPLE REVERSING DIGITS (again)
  • void main()
  • LinkedStack MyStack
  • char InputChar '0'
  • cout ltlt "Enter digits to display in
    reverse "
  • while (isdigit(InputChar))
  • cin gtgt InputChar
  • if (isDigit(InputChar))
  • MyStack.Push(InputChar)
  • char OutputChar
  • while (!MyStack.IsEmpty())
  • OutputChar MyStack.Pop()
  • cout ltlt OutputChar ltlt " "
  • cout ltlt endl

11
QUESTIONS?
  • Read P. 389 - 444 on QUEUES
Write a Comment
User Comments (0)
About PowerShow.com