CMSC 341 Deques Stacks and Queues 2202006 The

  • Slides: 36
Download presentation
CMSC 341 Deques, Stacks and Queues 2/20/2006

CMSC 341 Deques, Stacks and Queues 2/20/2006

The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”.

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 2/20/2006 2

Insert. At. Front Remove. From. Front 2/20/2006 Remove. From. Back Insert. At. Back 3

Insert. At. Front Remove. From. Front 2/20/2006 Remove. From. Back Insert. At. Back 3

Front 2/20/2006 Back 4

Front 2/20/2006 Back 4

Insert. At. Front(10) 10 Front 2/20/2006 Back 5

Insert. At. Front(10) 10 Front 2/20/2006 Back 5

Insert. At. Front(5) 5 Front 2/20/2006 10 Back 6

Insert. At. Front(5) 5 Front 2/20/2006 10 Back 6

Insert. At. Back(20) 5 Front 2/20/2006 10 20 Back 7

Insert. At. Back(20) 5 Front 2/20/2006 10 20 Back 7

Insert. At. Back(remove. From. Front()) 10 Front 2/20/2006 20 5 Back 8

Insert. At. Back(remove. From. Front()) 10 Front 2/20/2006 20 5 Back 8

Adapting Lists to Implement Deques Adapter Design Pattern • Allow a client to use

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). 2/20/2006 9

Client (Deque user) the. Deque. Insert. At. Front( 10 ) Deque (adapter) the. List.

Client (Deque user) the. Deque. Insert. At. Front( 10 ) Deque (adapter) the. List. push_front( 10 ) ; List (adaptee) 2/20/2006 10

Deque. h #include “List. h” template <typename Object> class Deque { public: Deque(); Deque(const

Deque. h #include “List. h” template <typename Object> class Deque { public: Deque(); Deque(const Deque &deq); ~Deque(); bool Is. Empty() const; void Make. Empty(); void Insert. At. Front(const Object &x); void Insert. At. Back(const Object &x); Object Remove. From. Front(); Object Remove. From. Back(); Object Front( ) const; Object Back( ) const; const Deque &operator=(const Deque &deq); private: List<Object> m_the. List; }; 2/20/2006 11

Deque methods template <typename Object> Deque<Object>: : Deque() { // no code } template

Deque methods template <typename Object> Deque<Object>: : Deque() { // no code } template <typename Object> Deque<Object>: : Deque(const Deque &deq) { m_the. List = deq. m_the. List; } template <typename Object> Deque<Object>: : ~Deque() { // no code } 2/20/2006 12

Deque methods (2) template <typename Object> bool Deque<Object>: : Is. Empty( ) const {

Deque methods (2) template <typename Object> bool Deque<Object>: : Is. Empty( ) const { return m_the. List. empty( ); } template <typename Object> void Deque<Object>: : Make. Empty ( ) { m_the. List. clear( ); } 2/20/2006 13

Deque methods (3) template <typename Object> Object Deque<Object>: : Remove. From. Front( ) {

Deque methods (3) template <typename Object> Object Deque<Object>: : Remove. From. Front( ) { if (is. Empty()) throw Deque. Exception(“remove on empty deque”); Object tmp = m_the. List. front( ); m_the. List. pop_front( ); return tmp; } template <typename Object> void Deque<Object>: : Insert. At. Front(const Object &x) { m_the. List. push_front( x ); } 2/20/2006 14

Deque methods (4) template <typename Object> Object Deque<Object>: : Remove. From. Back( ) {

Deque methods (4) template <typename Object> Object Deque<Object>: : Remove. From. Back( ) { if (is. Empty()) throw Deque. Exception(“remove on empty deque”); Object tmp = m_the. List. back( ); m_the. List. pop_back( ); return tmp; } template <typename Object> void Deque<Object>: : Insert. At. Back(const Object &x) { m_the. List. push_back( x ); } 2/20/2006 15

Deque methods (5) // examine the element at the front of the Deque template

Deque methods (5) // examine the element at the front of the Deque template <typename Object> Object Deque<Object>: : Front( ) const { if (is. Empty()) throw Deque. Exception(“front on empty deque”); return m_the. List. front( ); } // examine the element at the back of the Deque template <typename Object> Object Deque<Object>: : Back( ) const { if (is. Empty()) throw Deque. Exception(“back on empty deque”); return m_the. List. back( ); } 2/20/2006 16

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

Deque methods (6) template <typename Object> const Deque<Object> &Deque<Object>: : operator=(const Deque &deq) { if (this != &deq) m_the. List = deq. m_the. List; return *this; } 2/20/2006 17

Deque. Exception. h class Deque. Exception { public: Deque. Exception(); // Message is the

Deque. Exception. h class Deque. Exception { public: Deque. Exception(); // Message is the empty string Deque. Exception(const string & error. Msg); Deque. Exception(const Deque. Exception & ce); ~Deque. Exception(); const Deque. Exception & operator=(const Deque. Exception & ce); const string & error. Msg() const; // Accessor for msg private: string m_msg; }; 2/20/2006 18

Deque. Exception. cpp Deque. Exception: : Deque. Exception( ){ /* no code */ }

Deque. Exception. cpp Deque. Exception: : Deque. Exception( ){ /* no code */ } Deque. Exception: : Deque. Exception(const string & error. Msg) { m_msg = error. Msg; } Deque. Exception: : Deque. Exception(const Deque. Exception &ce) { m_msg = ce. error. Msg(); } Deque. Exception: : ~Deque. Exception( ){ /* no code } 2/20/2006 19

Deque. Exception. cpp (cont’d) const Deque. Exception & Deque. Exception: : operator=(const Deque. Exception

Deque. Exception. cpp (cont’d) const Deque. Exception & Deque. Exception: : operator=(const Deque. Exception & ce) { if (this == &ce) return *this; // don't assign to itself m_msg = ce. error. Msg(); return *this; } const string & Deque. Exception: : error. Msg() const { return m_msg; } 2/20/2006 20

Test. Deque. cpp int main () { Deque<int> deq; deq. Insert. At. Back(1); deq.

Test. Deque. cpp int main () { Deque<int> deq; deq. Insert. At. Back(1); deq. Insert. At. Back(2); Print. Deque(deq); Deque<int> otherdeq; otherdeq = deq; Print. Deque(otherdeq); cout << deq. remove. From. Front( ) << endl; 2/20/2006 21

Test. Deque. C (cont) Print. Deque(deq); Print. Deque(otherdeq); try { deq. Remove. From. Front(

Test. Deque. C (cont) Print. Deque(deq); Print. Deque(otherdeq); try { deq. Remove. From. Front( ); } catch (Deque. Exception & e){ cout << e. error. Msg( ) << endl; } return 0; } 2/20/2006 22

Queue ADT Restricted Deque only add to end only remove from front Examples line

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 2/20/2006 23

Client (Queue user) the. Q. Enqueue( 10 ) Queue (adapter) the. Deque. Insert. At.

Client (Queue user) the. Q. Enqueue( 10 ) Queue (adapter) the. Deque. Insert. At. Back( 10 ) Deque (adapter/adaptee) the. List. push_back( 10 ) ; List (adaptee) 2/20/2006 24

Queue. h template <typename Object> class Queue { public: Queue( ); ~Queue( ); bool

Queue. h template <typename Object> class Queue { public: Queue( ); ~Queue( ); bool Is. Empty( ) const; void Make. Empty( ); Object Dequeue( ); void Enqueue (const Object & x); private: Deque<Object> m_the. Deque; }; 2/20/2006 25

Queue methods template <typename Object> Queue<Object>: : Queue( ) { /* no code */

Queue methods template <typename Object> Queue<Object>: : Queue( ) { /* no code */ } template <typename Object> Queue<Object>: : ~Queue( ) { /* no code * / } template <typename Object> void Queue<Object>: : Make. Empty( ) { m_the. Deque. Make. Empty( ); } 2/20/2006 26

Queue methods (2) template <typename Object> void Queue<Object>: : Enqueue(const Object &x) { m_the.

Queue methods (2) template <typename Object> void Queue<Object>: : Enqueue(const Object &x) { m_the. Deque. Insert. At. Back( x ); } template <typename Object> Object Queue<Object>: : Dequeue( ) { return m_the. Deque. Remove. From. Front( ); } 2/20/2006 27

An Alternative Queue Implementation template <typename Object> class Queue { public: Queue(int capacity =

An Alternative Queue Implementation template <typename Object> class Queue { public: Queue(int capacity = 10); ~Queue(); bool Is. Empty( ) const; bool Is. Full ( ) const; void Make. Empty( ); Object Dequeue( ); void Enqueue( const Object & x ); private: vector<Object> m_the. Array; int m_current. Size; int m_front; int m_back; void Increment( int &x ); }; 2/20/2006 28

Alternate Queue methods template <typename Object> Queue<Object>: : Queue( int capacity ) : m_the.

Alternate Queue methods template <typename Object> Queue<Object>: : Queue( int capacity ) : m_the. Array( capacity ) { Make. Empty( ); } // make queue logically empty template <typename Object> void Queue<Object>: : Make. Empty( ) { m_current. Size = 0; m_front = 0; m_back = -1; } 2/20/2006 29

Alternate Queue methods (2) template <typename Object> void Queue<Object>: : Enqueue(const Object &x) {

Alternate Queue methods (2) template <typename Object> void Queue<Object>: : Enqueue(const Object &x) { if ( Is. Full( ) ) throw Overflow( ); Increment (m_back); m_the. Array[m_back] = x; m_current. Size++; } template <typename Object> void Queue<Object>: : Increment( int &x ) { if ( ++x == m_the. Array. size( ) ) x = 0; } 2/20/2006 30

Alternate Queue methods (3) template <typename Object> Object Queue<Object>: : Dequeue( ) { if

Alternate Queue methods (3) template <typename Object> Object Queue<Object>: : Dequeue( ) { if ( Is. Empty( ) ) throw Underflow( ); m_current. Size--; Object front. Item = m_the. Array[m_front]; Increment(m_front); return front. Item; } 2/20/2006 31

Stack ADT Restricted Deque only add to top (front) only remove from top (front)

Stack ADT Restricted Deque only add to top (front) only remove from top (front) Examples pile of trays partial results local state balancing parentheses Implement as an adapter of Deque 2/20/2006 32

Client (Stack user) the. Stack. Push( 10 ) Queue (adapter) the. Deque. Insert. At.

Client (Stack user) the. Stack. Push( 10 ) Queue (adapter) the. Deque. Insert. At. Front( 10 ) Deque (adapter/adaptee) the. List. push_front( 10 ) ; List (adaptee) 2/20/2006 33

Stack. h template <typename Object> class Stack { public: Stack( ); ~Stack( ); bool

Stack. h template <typename Object> class Stack { public: Stack( ); ~Stack( ); bool Is. Empty( ) const; void Make. Empty( ); void Pop( ); void Push( const Object &x ); Object Top( ) const; private: Deque<Object> m_the. Deque; }; 2/20/2006 34

Stack methods template <typename Object> Stack<Object>: : Stack( ) { /* no code */

Stack methods template <typename Object> Stack<Object>: : Stack( ) { /* no code */ } template <typename Object> Stack<Object>: : ~Stack( ) { /* no code */ } template <typename Object> void Stack<Object>: : Make. Empty( ) { m_the. Deque. Make. Empty( ); } 2/20/2006 35

Stack methods (2) // “insert” a new element at the top of the stack

Stack methods (2) // “insert” a new element at the top of the stack template <typename Object> void Stack<Object>: : Push( const Object &x ) { m_the. Deque. Insert. At. Front( x ); } // remove the element at the top of the stack template <typename Object> Object Stack<Object>: : Pop( ) { return m_the. Deque. Remove. From. Front( ); } // return the element at the top of the stack template <typename Object> Object Stack<Object>: : Top( ) const { return m_the. Deque. Front( ); } 2/20/2006 36