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.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Queues CS 308 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: –Elements are added at.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CS Data Structures II Review COSC 2006 April 14, 2017
Doubly-Linked Lists Same basic functions operate on list Each node has a forward and backward link: What advantages does a doubly-linked list offer? 88.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Stacks.
Queues.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Chapter 16 Stacks and Queues Saurav Karmakar Spring 2007.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Stack and Queue.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub CSC 113 King Saud University College of Computer and Information Sciences Department of Computer Science Chapter.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 Linked Stack Chapter 4. 2 Linked Stack We can implement a stack as a linked list. Same operations. No fixed maximum size. Stack can grow indefinitely.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
Chapter 16 Stacks & Queues. Objective In this chapter we will learn:  Stacks  Queues  Different implementations (arrays and linked list) of both 
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
CMSC 202 Stacks and Queues. What’s a Queue? A queue is a linear collection of homogeneous data in which items added to the queue must be placed at the.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
CMSC 341 Linked Lists, Stacks and Queues. 8/3/2007 UMBC CMSC 341 LSQ 2 Implementing Your Own Linked List To create a doubly linked list as seen below.
1 Queues. 2 Queue Which of the following cases use similar structures? Cars lined up at a tollgate Cars on a 4-lane highway Customers at supermarket check-out.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Data Structures and Algorithm Analysis Dr. Ken Cosh Stacks ‘n’ Queues.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
1 Queues Chapter 4. 2 Objectives You will be able to Describe a queue as an ADT. Build a dynamic array based implementation of a queue ADT.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Unit 2 - Stack and Queue Slides 18 to end. Queue CMPT Anne Lavergne18.
Mark Redekopp David Kempe
Programming Abstractions
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Lists The List ADT.
Stack and Queue APURBO DATTA.
Linked Lists, Stacks and Queues Textbook Sections
Chapter 19: Stacks and Queues.
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Deques, Stacks and Queues
Stacks and Queues 1.
CMSC 341 Stacks and Queues 4/17/2019.
CMSC 341 Stacks and Queues 4/17/2019.
Stacks.
CMSC 341 Stacks and Queues 4/29/2019.
C++ Plus Data Structures
Queues.
CMSC 341 Lecture 7.
Stacks, Queues, and Deques
CMPT 225 Lecture 8 – Queue.
Data Structures & Programming
Data Structures & Programming
Presentation transcript:

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 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/20063 RemoveFromBack InsertAtFront RemoveFromFront InsertAtBack

2/20/20064 FrontBack

2/20/20065 FrontBack InsertAtFront(10) 10

2/20/20066 FrontBack InsertAtFront(5) 510

2/20/20067 FrontBack InsertAtBack(20) 51020

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

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).

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

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

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

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

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

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

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

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

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

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

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

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

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

2/20/ 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/ Client (Queue user) Deque (adapter/adaptee) List (adaptee) theQ.Enqueue( 10 ) theList.push_back( 10 ) ; Queue (adapter) theDeque.InsertAtBack( 10 )

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

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

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

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

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

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

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

2/20/ 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/ Client (Stack user) Deque (adapter/adaptee) List (adaptee) theStack.Push( 10 ) theList.push_front( 10 ) ; Queue (adapter) theDeque.InsertAtFront( 10 )

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

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

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