Presentation is loading. Please wait.

Presentation is loading. Please wait.

Deques, Stacks and Queues

Similar presentations


Presentation on theme: "Deques, Stacks and Queues"— Presentation transcript:

1 Deques, Stacks and Queues
CMSC 341 Deques, Stacks and Queues 11/17/2018

2 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 11/17/2018

3 insertAtFront removeFromBack removeFromFront insertAtBack 11/17/2018

4 Front Back 11/17/2018

5 insertAtFront(10) 10 Front Back 11/17/2018

6 insertAtFront(5) 5 10 Front Back 11/17/2018

7 insertAtBack(20) 5 10 20 Front Back 11/17/2018

8 insertAtBack(removeFromFront())
10 20 5 Front Back 11/17/2018

9 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 11/17/2018

10 Client Deque (adapter) List (adaptee) theDeque.insertAtFront(10)
theList.insert(10, theList.zeroth()) List (adaptee) 11/17/2018

11 Deque.H #include “LinkedList.H” template <class 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 rremoveFromBack(); const Deque &operator=(const Deque &deq); Private List data member which stores all stack elements. Operations are same as those in text. One difference, on an empty stack the following are illegal: top pop topAndPop 11/17/2018

12 Deque.H (cont) private: List<Object> m_theList; }; 11/17/2018
Private data member that is an iterator. Used to provide position for insertion (push). It never changes. 11/17/2018

13 New List Methods Assumed to Exist
ListItr<Object> last( ) Returns iterator that points to last object in list void remove(ListItr<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 11/17/2018

14 Deque.C template <class Object> Deque<Object>::Deque() {
// no code } Deque<Object>::Deque(const Deque &deq) m_theList = deq.m_theList; Deque<Object>::~Deque() Top() uses first() to get top element. Each time top is called, an iterator is constructed. 11/17/2018

15 Deque.C (cont) template <class Object>
bool Deque<Object>::isEmpty( ) const { return m_theList.isEmpty( ); } void Deque<Object>::makeEmpty ( ) m_theList.makeEmpty(); Top() uses first() to get top element. Each time top is called, an iterator is constructed. 11/17/2018

16 Deque.C (cont) template <class Object>
Object Deque<Object>::removeFromFront( ) { if (isEmpty()) throw DequeException(“remove on empty deque”); Object tmp = m_theList.first().retrieve(); m_theList.remove(m_theList.first( )); return tmp; } void Deque<Object>::insertAtFront(const Object &x) m_theList.insert(x, m_theList.zeroth( )); Top() uses first() to get top element. Each time top is called, an iterator is constructed. 11/17/2018

17 Deque.C (cont) template <class Object>
Object Deque<Object>::removeFromBack( ) { if (isEmpty()) throw DequeException(“remove on empty deque”); Object tmp = m_theList.last().retrieve(); m_theList.remove(m_theList.last( )); return tmp; } void Deque<Object>::insertAtBack(const Object &x) m_theList.insert(x, m_theList.last( )); Top() uses first() to get top element. Each time top is called, an iterator is constructed. 11/17/2018

18 Deque.C (cont) template <class Object>
const Deque<Object> &Deque<Object>:: operator=(const Deque &deq) { if (this != &deq) m_theList = deq.m_theList; return *this; } Top() uses first() to get top element. Each time top is called, an iterator is constructed. 11/17/2018

19 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; }; 11/17/2018

20 DequeException.C 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 } 11/17/2018

21 DequeException.C (cont)
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; 11/17/2018

22 TestDeque.C int main (){ Deque<int> deq; deq.insertAtBack(1);
printDeque(deq); Deque<int> otherdeq; otherdeq = deq; printDeque(otherdeq); cout << deq.removeFromFront() << endl; 11/17/2018

23 TestDeque.C (cont) printDeque(deq); printDeque(otherdeq); try {
deq.removeFromFront(); } catch (DequeException & e){ cout << e.errorMsg() << endl; 11/17/2018

24 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 Implementing a queue could use similar approach to stack want efficient access to both ends could use circular doubly-linked lists 11/17/2018

25 Queue.H template <class Object> class Queue { public: Queue();
bool isEmpty() const; void makeEmpty(); Object dequeue(); void enqueue (const Object & x); private: Deque<Object> m_theDeque; } 11/17/2018

26 Queue.C template <class Object> Queue<Object>::Queue()
{ /* no code */ } Queue<Object>::~Queue() { /* no code * / } void Queue<Object>::makeEmpty( ) { m_theDeque.makeEmpty(); } 11/17/2018

27 Queue.C (cont’d) template <class Object>
void Queue<Object>::enqueue(const Object &x) { m_theDeque.insertAtBack(x); } Object Queue<Object>::dequeue() return m_theDeque.removeFromFront( ); . 11/17/2018

28 An Alternative Queue.H template <class Object> class Queue {
public: Queue(int capacity = 10); ~Queue(); bool isEmpty() 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); } 11/17/2018

29 Queue.C template <class Object>
Queue<Object>::Queue( int capacity ) : m_theArray( capacity ) { makeEmpty( ); } // make queue logically empty void Queue<Object>::makeEmpty( ) m_currentSize = 0; m_front = 0; m_back = -1; 11/17/2018

30 Queue.C (cont’d) template <class Object>
void Queue<Object>::enqueue(const Object &x) { if (isFull()) throw Overflow(); increment (m_back); m_theArray[m_back] = x; m_currentSize++; } void Queue<Object>::increment(int &x) if (++x == m_theArray.size()) x = 0; . 11/17/2018

31 Queue.C (cont) template <class Object>
Object Queue<Object>::dequeue() { if (isEmpty()) throw Underflow(); m_currentSize--; Object frontItem = m_theArray[m_front]; increment(m_front); return frontItem; } Top() uses first() to get top element. Each time top is called, an iterator is constructed. 11/17/2018

32 theArray 11/17/2018 Template<class Object>
void Queue<Object>::makeEmpty() { currentSize = 0; front = 0; back = -1; } 11/17/2018

33 Stack ADT Restricted List only add to top only remove from top
Examples pile of trays partial results local state Implement using a Deque 11/17/2018

34 Stack.H Object pop(); template <class Object> class Stack {
public: Stack( ); ~Stack( ); bool isEmpty( ) const; void makeEmpty( ); Object pop(); void push(const Object &x); private: Deque<Object> m_theDeque; }; Private List data member which stores all stack elements. Operations are same as those in text. One difference, on an empty stack the following are illegal: top pop topAndPop 11/17/2018

35 Stack.C template <class Object> Stack<Object>::Stack()
{ /* no code */ } Stack<Object>::~Stack() { /* no code */ } void Stack<Object>::makeEmpty( ) { m_theDeque.makeEmpty(); } 11/17/2018

36 Stack.C (cont’d) template <class Object>
void Stack<Object>::push(const Object &x) { m_theDeque.insertAtFront(x); } Object Stack<Object>::pop() return m_theDeque.removeFromFront( ); . 11/17/2018


Download ppt "Deques, Stacks and Queues"

Similar presentations


Ads by Google