cmsc 341 deques, stacks and queues. 2/20/20062 the double-ended queue adt a deque (rhymes with...

36
CMSC 341 Deques, Stacks and Queues

Upload: benedict-obrien

Post on 17-Jan-2016

221 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

CMSC 341

Deques, Stacks and Queues

Page 2: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 2

The Double-Ended Queue ADT

A Deque (rhymes with “check”) is a “Double Ended QUEue”.

A Deque is a restricted List which supports only

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

Page 3: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 3

RemoveFromBackInsertAtFront

RemoveFromFront InsertAtBack

Page 4: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 4

Front Back

Page 5: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 5

Front Back

InsertAtFront(10)

10

Page 6: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 6

Front Back

InsertAtFront(5)

5 10

Page 7: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 7

Front Back

InsertAtBack(20)

5 10 20

Page 8: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 8

Front Back

InsertAtBack(removeFromFront())

10 20 5

Page 9: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 9

Adapting Lists to Implement Deques

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

• In this case, the Deque is an adapter for the List. The client (user) calls methods of the Deque which in turn calls appropriate List method(s).

Page 10: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 10

Client (Deque user)

Deque (adapter)

List (adaptee)

theDeque.InsertAtFront( 10 )

theList.push_front( 10 ) ;

Page 11: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 11

Deque.h#include “List.h”template <typename Object>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 RemoveFromBack();Object Front( ) const;Object Back( ) const;const Deque &operator=(const Deque &deq);

private:List<Object> m_theList;

};

Page 12: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 12

Deque methodstemplate <typename Object>Deque<Object>::Deque(){

// no code}

template <typename Object>Deque<Object>::Deque(const Deque &deq){

m_theList = deq.m_theList;}

template <typename Object>Deque<Object>::~Deque(){

// no code}

Page 13: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 13

Deque methods (2)

template <typename Object>bool Deque<Object>::IsEmpty( ) const{

return m_theList.empty( );}

template <typename Object>void Deque<Object>::MakeEmpty ( ){

m_theList.clear( );}

Page 14: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 14

Deque methods (3)

template <typename Object>Object Deque<Object>::RemoveFromFront( ){

if (isEmpty())throw DequeException(“remove on empty deque”);

Object tmp = m_theList.front( );m_theList.pop_front( );return tmp;

}

template <typename Object>void Deque<Object>::InsertAtFront(const Object &x){

m_theList.push_front( x );}

Page 15: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 15

Deque methods (4)

template <typename Object>Object Deque<Object>::RemoveFromBack( ) {

if (isEmpty())throw DequeException(“remove on empty deque”);

Object tmp = m_theList.back( );m_theList.pop_back( );return tmp;

}

template <typename Object>void Deque<Object>::InsertAtBack(const Object &x){

m_theList.push_back( x );}

Page 16: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 16

Deque methods (5)

// examine the element at the front of the Dequetemplate <typename Object>Object Deque<Object>::Front( ) const{

if (isEmpty())throw DequeException(“front on empty deque”);

return m_theList.front( );}

// examine the element at the back of the Dequetemplate <typename Object>Object Deque<Object>::Back( ) const{

if (isEmpty())throw DequeException(“back on empty deque”);

return m_theList.back( );}

Page 17: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 17

Deque methods (6)template <typename Object>const Deque<Object> &Deque<Object>::operator=(const Deque &deq){

if (this != &deq)m_theList = deq.m_theList;

return *this;}

Page 18: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 18

DequeException.hclass 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 m_msg;

};

Page 19: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 19

DequeException.cpp

DequeException::DequeException( ){ /* no code */ }

DequeException::DequeException(const string & errorMsg)

{

m_msg = errorMsg;

}

DequeException::DequeException(const DequeException &ce)

{

m_msg = ce.errorMsg();

}

DequeException::~DequeException( ){ /* no code }

Page 20: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 20

DequeException.cpp (cont’d)

const DequeException &

DequeException::operator=(const DequeException & ce)

{

if (this == &ce)

return *this; // don't assign to itself

m_msg = ce.errorMsg();

return *this;

}

const string & DequeException::errorMsg() const

{

return m_msg;

}

Page 21: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 21

TestDeque.cppint main (){ Deque<int> deq;

deq.InsertAtBack(1); deq.InsertAtBack(2); PrintDeque(deq); Deque<int> otherdeq; otherdeq = deq; PrintDeque(otherdeq); cout << deq.removeFromFront( ) << endl; cout << deq.removeFromFront( ) << endl;

Page 22: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 22

TestDeque.C (cont) PrintDeque(deq);

PrintDeque(otherdeq);

try {

deq.RemoveFromFront( );

}

catch (DequeException & e){

cout << e.errorMsg( ) << endl;

}

return 0;

}

Page 23: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 23

Queue ADT

Restricted Deque

only add to end

only remove from front

Examples

line waiting for service

jobs waiting to print

Implement as an adapter of Deque

Page 24: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 24

Client (Queue user)

Deque (adapter/adaptee)

List (adaptee)

theQ.Enqueue( 10 )

theList.push_back( 10 ) ;

Queue (adapter)

theDeque.InsertAtBack( 10 )

Page 25: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 25

Queue.h

template <typename Object>class Queue{ public:

Queue( );~Queue( );bool IsEmpty( ) const;void MakeEmpty( );Object Dequeue( );void Enqueue (const Object & x);

private:Deque<Object> m_theDeque;

};

Page 26: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 26

Queue methods

template <typename Object>

Queue<Object>::Queue( )

{ /* no code */ }

template <typename Object>

Queue<Object>::~Queue( )

{ /* no code * / }

template <typename Object>

void Queue<Object>::MakeEmpty( )

{

m_theDeque.MakeEmpty( );

}

Page 27: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 27

Queue methods (2)

template <typename Object>

void Queue<Object>::Enqueue(const Object &x)

{

m_theDeque.InsertAtBack( x );

}

template <typename Object>

Object Queue<Object>::Dequeue( )

{

return m_theDeque.RemoveFromFront( );

}

Page 28: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 28

An Alternative Queue Implementationtemplate <typename Object>class Queue {

public:Queue(int capacity = 10);~Queue();bool IsEmpty( ) const;bool IsFull ( ) const;void MakeEmpty( );Object Dequeue( );void Enqueue( const Object & x );

private:vector<Object> m_theArray;int m_currentSize;int m_front;int m_back;void Increment( int &x );

};

Page 29: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 29

Alternate Queue methodstemplate <typename Object>

Queue<Object>::Queue( int capacity ): m_theArray( capacity )

{

MakeEmpty( );

}

// make queue logically empty

template <typename Object>

void Queue<Object>::MakeEmpty( )

{

m_currentSize = 0;

m_front = 0;

m_back = -1;

}

Page 30: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 30

Alternate Queue methods (2)template <typename Object>

void Queue<Object>::Enqueue(const Object &x)

{

if ( IsFull( ) )

throw Overflow( );

Increment (m_back);

m_theArray[m_back] = x;

m_currentSize++;

}

template <typename Object>

void Queue<Object>::Increment( int &x )

{

if ( ++x == m_theArray.size( ) )

x = 0;

}

Page 31: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 31

Alternate Queue methods (3)template <typename Object>

Object Queue<Object>::Dequeue( )

{

if ( IsEmpty( ) )

throw Underflow( );

m_currentSize--;

Object frontItem = m_theArray[m_front];

Increment(m_front);

return frontItem;

}

Page 32: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 32

Stack ADT

Restricted Dequeonly add to top (front)only remove from top (front)

Examplespile of trayspartial resultslocal statebalancing parentheses

Implement as an adapter of Deque

Page 33: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 33

Client (Stack user)

Deque (adapter/adaptee)

List (adaptee)

theStack.Push( 10 )

theList.push_front( 10 ) ;

Queue (adapter)

theDeque.InsertAtFront( 10 )

Page 34: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 34

Stack.h

template <typename Object>class Stack {public:

Stack( );~Stack( );bool IsEmpty( ) const;void MakeEmpty( );

void Pop( );void Push( const Object &x );Object Top( ) const;

private:Deque<Object> m_theDeque;

};

Page 35: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 35

Stack methods

template <typename Object>

Stack<Object>::Stack( )

{ /* no code */ }

template <typename Object>

Stack<Object>::~Stack( )

{ /* no code */ }

template <typename Object>

void Stack<Object>::MakeEmpty( )

{

m_theDeque.MakeEmpty( );

}

Page 36: CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted

2/20/2006 36

Stack methods (2)// “insert” a new element at the top of the stacktemplate <typename Object>void Stack<Object>::Push( const Object &x ){

m_theDeque.InsertAtFront( x );}

// remove the element at the top of the stacktemplate <typename Object>Object Stack<Object>::Pop( ){

return m_theDeque.RemoveFromFront( );}// return the element at the top of the stacktemplate <typename Object>Object Stack<Object>::Top( ) const{

return m_theDeque.Front( );}