CMSC 341 - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

CMSC 341

Description:

CMSC 341 Deques, Stacks and Queues – PowerPoint PPT presentation

Number of Views:127
Avg rating:3.0/5.0
Slides: 37
Provided by: umb99
Category:
Tags: cmsc

less

Transcript and Presenter's Notes

Title: CMSC 341


1
CMSC 341
  • Deques, Stacks and Queues

2
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

3
insertAtFront
removeFromBack
removeFromFront
insertAtBack
4
Front
Back
5
insertAtFront(10)
10
Front
Back
6
insertAtFront(5)
Front
Back
7
insertAtBack(20)
Front
Back
8
insertAtBack(removeFromFront())
Front
Back
9
Adapting Lists to Implement Deques
  • List interface (Weiss)
  • Does not directly support Deque operations
  • Could support them with a little extra code
  • Any good list interface will support Deque
    operations
  • Adapter Design Pattern
  • Allow a client to use a class whose interface is
    different from the one expected by the client
  • Do not modify client or class, write adapter
    class that sits between them

10
Client
theDeque.insertAtFront(10)
Deque (adapter)
theList.insert(10, theList.zeroth())
List (adaptee)
11
Deque.H
  • include LinkedList.H
  • template ltclass Objectgt
  • class Deque
  • public
  • Deque()
  • Deque(const Deque deq)
  • Deque()
  • bool isEmpty() const
  • void makeEmpty()
  • void insertAtFront(const Object x)
  • void insertAtBack(const Object x)
  • Object removeFromFront()
  • Object rremoveFromBack()
  • const Deque operator(const Deque deq)

12
Deque.H (cont)
  • private
  • ListltObjectgt _theList

13
New List Methods Assumed to Exist
  • ListItrltObjectgt last()
  • Returns iterator point to last object in list
  • remove(ListItrltObjectgt itr)
  • Removes the object pointed to by itr
  • What is the state of itr after remove?
  • What are the complexity of last() and remove()?
  • Singly linked
  • Doubly linked

14
Deque.C
  • template ltclass Objectgt
  • DequeltObjectgtDeque()
  • template ltclass Objectgt
  • DequeltObjectgtDeque(const Deque deq)
  • _theList deq._theList
  • template ltclass Objectgt
  • DequeltObjectgtDeque()

15
Deque.C (cont)
  • template ltclass Objectgt
  • bool DequeltObjectgtisEmpty() const
  • return _theList.isEmpty()
  • template ltclass Objectgt
  • void DequeltObjectgtmakeEmpty ()
  • _theList.makeEmpty()

16
Deque.C (cont)
  • template ltclass Objectgt
  • Object DequeltObjectgtremoveFromFront()
  • if (isEmpty())
  • throw DequeException(remove on empty deque)
  • Object tmp _theList.zeroth().retrieve()
  • _theList.remove(_theList.zeroth())
  • return tmp
  • template ltclass Objectgt
  • void DequeltObjectgtinsertAtFront(const Object
    x)
  • _theList.insert(x, _theList.zeroth())

17
Deque.C (cont)
  • template ltclass Objectgt
  • Object DequeltObjectgtremoveFromBack()
  • if (isEmpty())
  • throw DequeException(remove on empty deque)
  • Object tmp _theList.last().retrieve()
  • _theList.remove(_theList.last())
  • return tmp
  • template ltclass Objectgt
  • void DequeltObjectgtinsertAtBack(const Object x)
  • _theList.insert(x, _theList.last())

18
Deque.C (cont)
  • template ltclass Objectgt
  • const DequeltObjectgt DequeltObjectgt
  • operator(const Deque deq)
  • if (this ! deq)
  • _theList deq._theList
  • return this

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

20
DequeException.C
  • DequeExceptionDequeException()
  • DequeExceptionDequeException(const string
    errorMsg)
  • _msg errorMsg
  • DequeExceptionDequeException(const
    DequeException ce)
  • _msg ce.errorMsg()
  • DequeExceptionDequeException()

21
DequeException.C (cont)
  • const DequeException
  • DequeExceptionoperator(const DequeException
    ce)
  • if (this ce)
  • return this // don't assign to itself
  • _msg ce.errorMsg()
  • return this
  • const string DequeExceptionerrorMsg() const
  • return _msg

22
TestDeque.C
  • int main ()
  • Dequeltintgt deq
  • deq.insertAtBack(1)
  • deq.insertAtBack(2)
  • printDeque(deq)
  • Dequeltintgt otherdeq
  • otherdeq deq
  • printDeque(otherdeq)
  • cout ltlt deq.removeFromFront() ltlt endl
  • cout ltlt deq.removeFromFront() ltlt endl

23
TestDeque.C (cont)
  • printDeque(deq)
  • printDeque(otherdeq)
  • try
  • deq.removeFromFront()
  • catch (DequeException e)
  • cout ltlt e.errorMsg() ltlt endl

24
Queue ADT
  • Restricted List
  • only add to end
  • only remove from front
  • Examples
  • line waiting for service
  • jobs waiting to print
  • Implement using a Deque

25
Queue.H
  • template ltclass Objectgt
  • class Queue
  • public
  • Queue()
  • Queue()
  • bool isEmpty() const
  • void makeEmpty()Object dequeue()void enqueue
    (const Object x)
  • private
  • DequeltObjectgt _theDeque

26
Queue.C
  • template ltclass Objectgt
  • QueueltObjectgtQueue()
  • Template ltclass Objectgt
  • QueueltObjectgtQueue()
  • template ltclass Objectgt
  • void QueueltObjectgtmakeEmpty( )
  • _theDeque.makeEmpty()

27
Queue.C (contd)
  • template ltclass Objectgt
  • void QueueltObjectgtenqueue(const Object x)
  • _theDeque.insertAtBack(x)
  • template ltclass Objectgt
  • Object QueueltObjectgtdequeue()
  • return _theDeque.removeFromFront(x)

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

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

30
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

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

32
theArray
33
Stack ADT
  • Restricted List
  • only add to top
  • only remove from top
  • Examples
  • pile of trays
  • partial results
  • local state
  • Implement using a Deque

34
Stack.H
  • template ltclass Objectgt
  • class Stack
  • public
  • Stack()
  • Stack()
  • bool isEmpty() const
  • void makeEmpty()
  • Object pop()
  • void push(const Object x)
  • private
  • DequeueltObjectgt _theDeque

35
Stack.C
  • template ltclass Objectgt
  • StackltObjectgtStack()
  • Template ltclass Objectgt
  • StackltObjectgtStack()
  • template ltclass Objectgt
  • void StackltObjectgtmakeEmpty( )
  • _theDeque.makeEmpty()

36
Stack.C (contd)
  • template ltclass Objectgt
  • void QueueltObjectgtpush(const Object x)
  • _theDeque.insertAtFont(x)
  • template ltclass Objectgt
  • Object QueueltObjectgtpop()
  • return _theDeque.removeFromFront(x)
Write a Comment
User Comments (0)
About PowerShow.com