CMSC 341 - PowerPoint PPT Presentation

About This Presentation
Title:

CMSC 341

Description:

throw StackException('pop on empty stack'); getList().remove(top()); template class Object ... Message is the empty string. StackException ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 24
Provided by: csee3
Category:
Tags: cmsc | on | pop | the | trays

less

Transcript and Presenter's Notes

Title: CMSC 341


1
CMSC 341
  • Stacks and Queues

2
Stack ADT
  • Restricted List
  • only add to top
  • only remove from top
  • Examples
  • pile of trays
  • partial results
  • local state

3
Relation to Previous List
  • Using inheritance
  • Stack Is-A List
  • Need to pay special intention that List
    operations are
  • properly performed
  • Using aggregation
  • Stack Has-A List

4
Stack.H
  • include LinkedList.H
  • template ltclass Objectgt
  • class Stack
  • public
  • Stack()
  • Stack(const Stack coll)
  • Stack()
  • bool isEmpty() const
  • bool isFull() const
  • const Object top() const
  • void makeEmpty()
  • void pop()
  • void push(const Object x)
  • Object topAndPop()
  • const Stack operator(const Stack stk)

5
Stack.H (cont)
  • protected
  • const ListltObjectgt getList() const
  • void setList(ListltObjectgt lst)
  • ListItrltObjectgt getZeroth()
  • private
  • ListltObjectgt _theList
  • ListItrltObjectgt _zeroth

6
Stack.C
  • template ltclass Objectgt
  • StackltObjectgtStack()
  • _zeroth getList().zeroth()
  • template ltclass Objectgt
  • StackltObjectgtStack(const Stack stk)
  • _theList stk.getList()
  • _zeroth getList().zeroth()
  • template ltclass Objectgt
  • StackltObjectgtStack()
  • template ltclass Objectgt
  • bool StackltObjectgtisEmpty() const
  • return getList().isEmpty()

7
Stack.C (cont)
  • template ltclass Objectgt
  • bool StackltObjectgtisFull()const
  • return false
  • template ltclass Objectgt
  • const Object StackltObjectgttop() const
  • if (isEmpty())
  • throw StackException(top on empty stack)
  • return getList().first().retrieve()
  • template ltclass Objectgt
  • void StackltObjectgtmakeEmpty ()
  • getList().makeEmpty()

8
Stack.C (cont)
  • template ltclass Objectgt
  • void StackltObjectgtpop()
  • if (isEmpty())
  • throw StackException(pop on empty stack)
  • getList().remove(top())
  • template ltclass Objectgt
  • void StackltObjectgtpush(const Object x)
  • getList().insert(x, getZeroth())
  • template ltclass Objectgt
  • Object StackltObjectgttopAndPop ()
  • if (isEmpty())
  • throw StackException(topAndPop on empty
    stack)
  • Object tmp top() pop()
  • return tmp

9
Stack.C (cont)
  • template ltclass Objectgt
  • const StackltObjectgt StackltObjectgt
  • operator(const Stack stk)
  • if (this ! stk)
  • setList(stk.getList())
  • setZeroth(getList().zeroth())
  • return this
  • template ltclass Objectgt
  • const ListltObjectgt StackltObjectgtgetList()
  • return _theList
  • template ltclass Objectgt
  • ListItrltObjectgt StackltObjectgtgetZeroth ()
  • return _zeroth

10
StackException.H
  • class StackException
  • public
  • StackException() // Message is the empty
    string
  • StackException(const string errorMsg)
  • StackException(const StackException ce)
  • StackException()
  • const StackException
  • operator(const StackException ce)
  • const string errorMsg() const // Accessor for
    msg
  • private
  • string _msg

11
StackException.C
  • StackExceptionStackException()
  • StackExceptionStackException(const string
    errorMsg)
  • _msg errorMsg
  • StackExceptionStackException(const
    StackException ce)
  • _msg ce.errorMsg()
  • StackExceptionStackException()

12
StackException.C (cont)
  • const StackException
  • StackExceptionoperator(const StackException
    ce)
  • if (this ce)
  • return this // don't assign to itself
  • _msg ce.errorMsg()
  • return this
  • const string StackExceptionerrorMsg() const
  • return _msg

13
TestStack.C
  • int main ()
  • Stackltintgt stk
  • stk.push(1)
  • stk.push(2)
  • printStack(stk)
  • Stackltintgt otherstk
  • otherstk stk
  • printStack(otherstk)
  • cout ltlt stk.topAndPop() ltlt endl
  • cout ltlt stk.topAndPop() ltlt endl

14
TestStack.C (cont)
  • printStack(stk)
  • printStack(otherstk)
  • try
  • stk.pop()
  • catch (StackException e)
  • cout ltlt e.errorMsg() ltlt endl

15
TestStack_aux.C
  • template ltclass Objectgt
  • void printStack( StackltObjectgt theStack )
  • StackltObjectgt tmp
  • if( theStack.isEmpty( ) )
  • cout ltlt "Empty stack" ltlt endl
  • return
  • while (theStack.isEmpty() false)
  • Object topObj theStack.top()
  • cout ltlt topObj
  • tmp.push(topObj) // save on other stack
  • theStack.pop()
  • if (theStack.isEmpty() false)
  • cout ltlt ", "
  • cout ltlt endl

16
TestStack_aux.C
  • while (tmp.isEmpty() false)
  • Object topObj tmp.top()
  • theStack.push(topObj)
  • tmp.pop()

17
Queue ADT
  • Restricted List
  • only add to end
  • only remove from front
  • Examples
  • line waiting for service
  • jobs waiting to print

18
Queue.H
  • template ltclass Objectgt
  • class Queue
  • public
  • explicit Queue(int capacity10)
  • bool isEmpty() const
  • bool isFull() const
  • const Object getFront() const
  • void makeEmpty()Object dequeue()void enqueue
    (const Object x)
  • private
  • vectorltObjectgt theArray
  • int currentSize
  • int front
  • int back
  • void increment (int x)

19
Queue.C
  • template ltclass Objectgt
  • QueueltObjectgtQueue( int capacity ) theArray(
    capacity )
  • makeEmpty( )
  • template ltclass Objectgt
  • void QueueltObjectgtmakeEmpty( )
  • currentSize 0
  • front 0
  • back -1

20
Queue.C (contd)
  • template ltclass Objectgt
  • void QueueltObjectgtenqueue(const Object x)
  • if (isFull())
  • throw Overflow()
  • increment (back)
  • theArrayback x
  • currentSize
  • template ltclass Objectgt
  • void QueueltObjectgtincrement(int x)
  • if (x theArray.size())
  • x 0

21
Queue.C (cont)
  • template ltclass Objectgt
  • Object QueueltObjectgtdequeue()
  • if (isEmpty())
  • throw Underflow()
  • currentSize--
  • Object frontItem theArrayfront
  • increment(front)
  • return frontItem

22
theArray
23
The Double-Ended Queue ADT
  • The double ended queue is referred to as a Deque
    (rhymes with check)
  • Restricted List
  • add to the end
  • remove from the end
  • add to the front
  • remove from the front
  • Stacks and Queues are often implemented using a
    deque
Write a Comment
User Comments (0)
About PowerShow.com