Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 CMSC 341 Deques, Stacks and Queues

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

3 2/20/20063 RemoveFromBack InsertAtFront RemoveFromFront InsertAtBack

4 2/20/20064 FrontBack

5 2/20/20065 FrontBack InsertAtFront(10) 10

6 2/20/20066 FrontBack InsertAtFront(5) 510

7 2/20/20067 FrontBack InsertAtBack(20) 51020

8 2/20/20068 FrontBack InsertAtBack(removeFromFront()) 10205

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

10 2/20/200610 Client (Deque user) Deque (adapter) List (adaptee) theDeque.InsertAtFront( 10 ) theList.push_front( 10 ) ;

11 2/20/200611 Deque.h #include “List.h” template 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 m_theList; };

12 2/20/200612 Deque methods template Deque ::Deque() { // no code } template Deque ::Deque(const Deque &deq) { m_theList = deq.m_theList; } template Deque ::~Deque() { // no code }

13 2/20/200613 Deque methods (2) template bool Deque ::IsEmpty( ) const { return m_theList.empty( ); } template void Deque ::MakeEmpty ( ) { m_theList.clear( ); }

14 2/20/200614 Deque methods (3) template Object Deque ::RemoveFromFront( ) { if (isEmpty()) throw DequeException(“remove on empty deque”); Object tmp = m_theList.front( ); m_theList.pop_front( ); return tmp; } template void Deque ::InsertAtFront(const Object &x) { m_theList.push_front( x ); }

15 2/20/200615 Deque methods (4) template Object Deque ::RemoveFromBack( ) { if (isEmpty()) throw DequeException(“remove on empty deque”); Object tmp = m_theList.back( ); m_theList.pop_back( ); return tmp; } template void Deque ::InsertAtBack(const Object &x) { m_theList.push_back( x ); }

16 2/20/200616 Deque methods (5) // examine the element at the front of the Deque template Object Deque ::Front( ) const { if (isEmpty()) throw DequeException(“front on empty deque”); return m_theList.front( ); } // examine the element at the back of the Deque template Object Deque ::Back( ) const { if (isEmpty()) throw DequeException(“back on empty deque”); return m_theList.back( ); }

17 2/20/200617 Deque methods (6) template const Deque &Deque :: operator=(const Deque &deq) { if (this != &deq) m_theList = deq.m_theList; return *this; }

18 2/20/200618 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 m_msg; };

19 2/20/200619 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 }

20 2/20/200620 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; }

21 2/20/200621 TestDeque.cpp int main () { Deque deq; deq.InsertAtBack(1); deq.InsertAtBack(2); PrintDeque(deq); Deque otherdeq; otherdeq = deq; PrintDeque(otherdeq); cout << deq.removeFromFront( ) << endl;

22 2/20/200622 TestDeque.C (cont) PrintDeque(deq); PrintDeque(otherdeq); try { deq.RemoveFromFront( ); } catch (DequeException & e){ cout << e.errorMsg( ) << endl; } return 0; }

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

24 2/20/200624 Client (Queue user) Deque (adapter/adaptee) List (adaptee) theQ.Enqueue( 10 ) theList.push_back( 10 ) ; Queue (adapter) theDeque.InsertAtBack( 10 )

25 2/20/200625 Queue.h template class Queue { public: Queue( ); ~Queue( ); bool IsEmpty( ) const; void MakeEmpty( ); Object Dequeue( ); void Enqueue (const Object & x); private: Deque m_theDeque; };

26 2/20/200626 Queue methods template Queue ::Queue( ) { /* no code */ } template Queue ::~Queue( ) { /* no code * / } template void Queue ::MakeEmpty( ) { m_theDeque.MakeEmpty( ); }

27 2/20/200627 Queue methods (2) template void Queue ::Enqueue(const Object &x) { m_theDeque.InsertAtBack( x ); } template Object Queue ::Dequeue( ) { return m_theDeque.RemoveFromFront( ); }

28 2/20/200628 An Alternative Queue Implementation template 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 m_theArray; int m_currentSize; int m_front; int m_back; void Increment( int &x ); };

29 2/20/200629 Alternate Queue methods template Queue ::Queue( int capacity ) : m_theArray( capacity ) { MakeEmpty( ); } // make queue logically empty template void Queue ::MakeEmpty( ) { m_currentSize = 0; m_front = 0; m_back = -1; }

30 2/20/200630 Alternate Queue methods (2) template void Queue ::Enqueue(const Object &x) { if ( IsFull( ) ) throw Overflow( ); Increment (m_back); m_theArray[m_back] = x; m_currentSize++; } template void Queue ::Increment( int &x ) { if ( ++x == m_theArray.size( ) ) x = 0; }

31 2/20/200631 Alternate Queue methods (3) template Object Queue ::Dequeue( ) { if ( IsEmpty( ) ) throw Underflow( ); m_currentSize--; Object frontItem = m_theArray[m_front]; Increment(m_front); return frontItem; }

32 2/20/200632 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

33 2/20/200633 Client (Stack user) Deque (adapter/adaptee) List (adaptee) theStack.Push( 10 ) theList.push_front( 10 ) ; Queue (adapter) theDeque.InsertAtFront( 10 )

34 2/20/200634 Stack.h template class Stack { public: Stack( ); ~Stack( ); bool IsEmpty( ) const; void MakeEmpty( ); void Pop( ); void Push( const Object &x ); Object Top( ) const; private: Deque m_theDeque; };

35 2/20/200635 Stack methods template Stack ::Stack( ) { /* no code */ } template Stack ::~Stack( ) { /* no code */ } template void Stack ::MakeEmpty( ) { m_theDeque.MakeEmpty( ); }

36 2/20/200636 Stack methods (2) // “insert” a new element at the top of the stack template void Stack ::Push( const Object &x ) { m_theDeque.InsertAtFront( x ); } // remove the element at the top of the stack template Object Stack ::Pop( ) { return m_theDeque.RemoveFromFront( ); } // return the element at the top of the stack template Object Stack ::Top( ) const { return m_theDeque.Front( ); }


Download ppt "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."

Similar presentations


Ads by Google