CMSC 341 Deques Stacks and Queues 10192021 1




































- Slides: 36

CMSC 341 Deques, Stacks and Queues 10/19/2021 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 10/19/2021 2

insert. At. Front remove. From. Front 10/19/2021 remove. From. Back insert. At. Back 3

Front 10/19/2021 Back 4

insert. At. Front(10) 10 Front 10/19/2021 Back 5

insert. At. Front(5) 5 Front 10/19/2021 10 Back 6

insert. At. Back(20) 5 Front 10/19/2021 10 20 Back 7

insert. At. Back(remove. From. Front()) 10 Front 10/19/2021 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 10/19/2021 9

Client the. Deque. insert. At. Front(10) Deque (adapter) the. List. insert(10, the. List. zeroth()) List (adaptee) 10/19/2021 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); 10/19/2021 11

Deque. H (cont) private: List<Object> m_the. List; }; 10/19/2021 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 10/19/2021 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 } 10/19/2021 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(); } 10/19/2021 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( )); } 10/19/2021 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( )); } 10/19/2021 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; } 10/19/2021 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; }; 10/19/2021 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 } 10/19/2021 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; } 10/19/2021 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; 10/19/2021 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; } } 10/19/2021 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 10/19/2021 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; } 10/19/2021 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(); } 10/19/2021 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( ); } 10/19/2021 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); } 10/19/2021 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; } 10/19/2021 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; } 10/19/2021 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; } 10/19/2021 31

the. Array 10/19/2021 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 10/19/2021 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; }; 10/19/2021 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(); } 10/19/2021 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( ); } 10/19/2021 36
Cqueue
Java stacks and queues
Java stack exercises
Java stacks and queues
Cmsc 341
Umbc cmsc 341
Cmsc 341
Cmsc 341 umbc
Cmsc student portal
Cmsc 341
Cmsc 341
Cmsc 341
Cmsc 341 umbc
Cmsc 341
Cmsc 341
Coastal features diagram
3 min quiz
Representation of queues
Ipcs unix
Adaptable priority queues
Pipes in rtos
Applications of priority queues
Types of queue in data structure
Queue definition
Erisone queues
2 stack pushdown automata
Barrier bars geography
Types of stacks
Stacks in data structures
Stacks+routined
6 stacks
Speed stacks spreads nationally in 1998.
Plant cells
Stacks internet
Angle stacks
Sda hymn 341
Ecma-341