CMSC 341 Deques Stacks and Queues 262022 1
- Slides: 36
CMSC 341 Deques, Stacks and Queues 2/6/2022 1
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 2/6/2022 2
insert. At. Front remove. From. Front 2/6/2022 remove. From. Back insert. At. Back 3
Front 2/6/2022 Back 4
insert. At. Front(10) 10 Front 2/6/2022 Back 5
insert. At. Front(5) 5 Front 2/6/2022 10 Back 6
insert. At. Back(20) 5 Front 2/6/2022 10 20 Back 7
insert. At. Back(remove. From. Front()) 10 Front 2/6/2022 20 5 Back 8
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 2/6/2022 9
Client the. Deque. insert. At. Front(10) Deque (adapter) the. List. insert(10, the. List. zeroth()) List (adaptee) 2/6/2022 10
Deque. H #include “Linked. List. H” template <class 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 rremove. From. Back(); const Deque &operator=(const Deque &deq); 2/6/2022 11
Deque. H (cont) private: List<Object> m_the. List; }; 2/6/2022 12
New List Methods Assumed to Exist List. Itr<Object> last( ) • Returns iterator that points to last object in list void remove(List. Itr<Object> 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 2/6/2022 13
Deque. C template <class Object> Deque<Object>: : Deque() { // no code } template <class Object> Deque<Object>: : Deque(const Deque &deq) { m_the. List = deq. m_the. List; } template <class Object> Deque<Object>: : ~Deque() { // no code } 2/6/2022 14
Deque. C (cont) template <class Object> bool Deque<Object>: : is. Empty( ) const { return m_the. List. is. Empty( ); } template <class Object> void Deque<Object>: : make. Empty ( ) { m_the. List. make. Empty(); } 2/6/2022 15
Deque. C (cont) template <class Object> Object Deque<Object>: : remove. From. Front( ) { if (is. Empty()) throw Deque. Exception(“remove on empty deque”); Object tmp = m_the. List. first(). retrieve(); m_the. List. remove(m_the. List. first( )); return tmp; } template <class Object> void Deque<Object>: : insert. At. Front(const Object &x) { m_the. List. insert(x, m_the. List. zeroth( )); } 2/6/2022 16
Deque. C (cont) template <class Object> Object Deque<Object>: : remove. From. Back( ) { if (is. Empty()) throw Deque. Exception(“remove on empty deque”); Object tmp = m_the. List. last(). retrieve(); m_the. List. remove(m_the. List. last( )); return tmp; } template <class Object> void Deque<Object>: : insert. At. Back(const Object &x) { m_the. List. insert(x, m_the. List. last( )); } 2/6/2022 17
Deque. C (cont) template <class Object> const Deque<Object> &Deque<Object>: : operator=(const Deque &deq) { if (this != &deq) m_the. List = deq. m_the. List; return *this; } 2/6/2022 18
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/6/2022 19
Deque. Exception. C 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/6/2022 20
Deque. Exception. C (cont) 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/6/2022 21
Test. Deque. C 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/6/2022 22
Test. Deque. C (cont) print. Deque(deq); print. Deque(otherdeq); try { deq. remove. From. Front(); } catch (Deque. Exception & e){ cout << e. error. Msg() << endl; } } 2/6/2022 23
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 2/6/2022 24
Queue. H template <class 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/6/2022 25
Queue. C template <class Object> Queue<Object>: : Queue() { /* no code */ } template <class Object> Queue<Object>: : ~Queue() { /* no code * / } template <class Object> void Queue<Object>: : make. Empty( ) { m_the. Deque. make. Empty(); } 2/6/2022 26
Queue. C (cont’d) template <class Object> void Queue<Object>: : enqueue(const Object &x) { m_the. Deque. insert. At. Back(x); } template <class Object> Object Queue<Object>: : dequeue() { return m_the. Deque. remove. From. Front( ); } 2/6/2022 27
An Alternative Queue. H template <class Object> class Queue { public: Queue(int capacity = 10); ~Queue(); bool is. Empty() 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/6/2022 28
Queue. C template <class Object> Queue<Object>: : Queue( int capacity ) : m_the. Array( capacity ) { make. Empty( ); } // make queue logically empty template <class Object> void Queue<Object>: : make. Empty( ) { m_current. Size = 0; m_front = 0; m_back = -1; } 2/6/2022 29
Queue. C (cont’d) template <class 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 <class Object> void Queue<Object>: : increment(int &x) { if (++x == m_the. Array. size()) x = 0; } 2/6/2022 30
Queue. C (cont) template <class 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/6/2022 31
the. Array 2/6/2022 32
Stack ADT Restricted List only add to top only remove from top Examples pile of trays partial results local state Implement using a Deque 2/6/2022 33
Stack. H template <class Object> class Stack { public: Stack( ); ~Stack( ); bool is. Empty( ) const; void make. Empty( ); Object pop(); void push(const Object &x); private: Deque<Object> m_the. Deque; }; 2/6/2022 34
Stack. C template <class Object> Stack<Object>: : Stack() { /* no code */ } template <class Object> Stack<Object>: : ~Stack() { /* no code */ } template <class Object> void Stack<Object>: : make. Empty( ) { m_the. Deque. make. Empty(); } 2/6/2022 35
Stack. C (cont’d) template <class Object> void Stack<Object>: : push(const Object &x) { m_the. Deque. insert. At. Front(x); } template <class Object> Object Stack<Object>: : pop() { return m_the. Deque. remove. From. Front( ); } 2/6/2022 36
- Python stack and queue
- Java stacks and queues
- Java stack exercises
- What are stacks
- Cmsc 341
- Cmsc 341 umbc
- Cmsc 341
- Umbc cmsc 341
- Cmsc student portal
- Cmsc 341
- Cmsc 341
- Umbc cmsc 341
- Umbc cmsc 341
- Cmsc 341
- Cmsc 341
- How waves are formed
- 3 min quiz
- Queue representation
- Ipcs unix
- Priority queue adt
- Pipes in rtos
- Applications of priority queues
- Gambar depan
- Queue definition
- Aganj
- Two-stack pda examples
- Terrace
- Types of stacks
- Stacks in data structures
- Stacks+routined
- 6 stacks
- Speedstacks com go
- The individual flattened stacks of membrane
- Stacks internet
- Angle stacks
- Sda hymn 341
- Ecma-341