Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. Chapter 7: Queues Data Abstraction & Problem Solving with C++

Slides:



Advertisements
Similar presentations
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
What is a Queue? A queue is a FIFO “first in, first out” structure.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Queue Overview Queue ADT Basic operations of queue
CS Data Structures II Review COSC 2006 April 14, 2017
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Stacks and Queues. Sample PMT online… Browse 1120/sumII05/PMT/2004_1/
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
CSS342: Queues1 Professor: Munehiro Fukuda. CSS342: Queues2 Topics Basic concepts of queue Queue implementation Queues used in –Applications –Operating.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 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,
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
CS Data Structures I Chapter 7 Queue I. 2 Topics Introduction Queue Application Implementation Linked List Array ADT List.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
Queues Lecture 4. Lecture Objectives  Learn about queues  Examine various queue operations  Learn how to implement a queue as an array  Learn how.
Chapter 7 A Queues. © 2004 Pearson Addison-Wesley. All rights reserved7 A-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear,
Queue Section 3 6/12/ The Abstract Data Type Queue A queue is a list from which items are deleted from one end (front) and into which items are.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Queues and Priority Queue Implementations Chapter 14 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Queues Chapter 8 (continued)
Chapter 7 Queues.
Data Abstraction & Problem Solving with C++
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
CC 215 Data Structures Queue ADT
Csc 2720 Data structure Instructor: Zhuojun Duan
CENG 213 Data Structure Queue 7/2/2018.
Chapter 4 Linked Lists.
The Abstract Data Type Queue
Chapter 4 Linked Lists
Queues.
Chapter 4 Linked Lists.
Queue and Priority Queue Implementations
CSC 143 Queues [Chapter 7].
Ch. 8 Queue Stack Queue milk queue stack
Chapter 4 Linked Lists.
Figure 7.1 Some queue operations. Figure 7.1 Some queue operations.
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues and Priority Queue Implementations
Queues Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues.
Presentation transcript:

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++ Fifth Edition by Frank M. Carrano

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver The Abstract Data Type Queue A queue –New items enter at the back, or rear, of the queue –Items leave from the front of the queue –First-in, first-out (FIFO) property The first item inserted into a queue is the first item to leave

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver The Abstract Data Type Queue Queues –Are appropriate for many real-world situations Example: A line to buy a movie ticket –Have applications in computer science Example: A request to print a document –Simulation A study to see how to reduce the wait involved in an application

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver The Abstract Data Type Queue ADT queue operations –Create an empty queue –Destroy a queue –Determine whether a queue is empty –Add a new item to the queue –Remove the item that was added earliest –Retrieve the item that was added earliest

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver The Abstract Data Type Queue Operation Contract for the ADT Queue isEmpty():boolean {query} enqueue(in newItem:QueueItemType) dequeue() dequeue(out queueFront:QueueItemType) getFront(out queueFront:QueueItemType) {query}

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver The Abstract Data Type Queue Figure 7-2 Some queue operations

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Reading a String of Characters A queue can retain characters in the order in which they are typed aQueue.createQueue() while (not end of line) { Read a new character ch aQueue.enqueue(ch) } // end while Once the characters are in a queue, the system can process them as necessary

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recognizing Palindromes A palindrome –A string of characters that reads the same from left to right as its does from right to left To recognize a palindrome, you can use a queue in conjunction with a stack –A stack reverses the order of occurrences –A queue preserves the order of occurrences

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recognizing Palindromes A nonrecursive recognition algorithm for palindromes –As you traverse the character string from left to right, insert each character into both a queue and a stack –Compare the characters at the front of the queue and the top of the stack

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recognizing Palindromes Figure 7-3 The results of inserting a string into both a queue and a stack

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Implementations of the ADT Queue An array-based implementation Possible implementations of a pointer-based queue –A linear linked list with two external references A reference to the front A reference to the back –A circular linked list with one external reference A reference to the back

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver A Pointer-Based Implementation Figure 7-4 A pointer-based implementation of a queue: (a) a linear linked list with two external pointers; (b) a circular linear linked list with one external pointer

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver A Pointer-Based Implementation Figure 7-7 Deleting an item from a queue of more than one item Figure 7-6 Inserting an item into an empty queue: (a) before insertion; (b) after insertion Figure 7-5 Inserting an item into a nonempty queue

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver QueueP.h */ #include "QueueException.h" typedef desired-type-of-queue-item QueueItemType; Queue * ADT queue - Pointer-based implementation. */ class Queue { public: Queue(); Queue(const Queue& Q); ~Queue(); bool isEmpty() const; void enqueue(const QueueItemType& newItem); void dequeue() ; void dequeue(QueueItemType& queueFront) ; void getFront(QueueItemType& queueFront) ;

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver private: struct QueueNode { QueueItemType item; QueueNode *next; }; // end QueueNode QueueNode *backPtr; QueueNode *frontPtr; }; // end Queue // End of header file.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver QueueP.cpp */ #include // for NULL #include // for assert #include // for bad_alloc #include "QueueP.h" // header file using namespace std; Queue::Queue() : backPtr(NULL), frontPtr(NULL) { } // end default constructor Queue::Queue(const Queue& Q) { // Implementation left as an exercise (Exercise 6). } // end copy constructor

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Queue::~Queue() { while (!isEmpty()) dequeue(); assert ( (backPtr == NULL) && (frontPtr == NULL) ); } // end destructor bool Queue::isEmpty() const { return backPtr == NULL; } // end isEmpty

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver void Queue::enqueue(const QueueItemType& newItem) { // create a new node QueueNode *newPtr = new QueueNode; // set data portion of new node newPtr->item = newItem; newPtr->next = NULL; // insert the new node if (isEmpty()) // insertion into empty queue frontPtr = newPtr; else // insertion into nonempty queue backPtr->next = newPtr; backPtr = newPtr; // new node is at back }

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver } // end enqueue

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver void Queue::dequeue() throw(QueueException) { if (isEmpty()) cout << “queue is empty”; else { // queue is not empty; remove front QueueNode *tempPtr = frontPtr; if (frontPtr == backPtr) // special case? { // yes, one node in queue frontPtr = NULL; backPtr = NULL; } else frontPtr = frontPtr->next; tempPtr->next = NULL; // defensive strategy delete tempPtr; } // end if } // end dequeue

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver void Queue::dequeue(QueueItemType& queueFront) { if (isEmpty()) cout << “empty queue, cannot delete”); else { // queue is not empty; retrieve front queueFront = frontPtr->item; dequeue(); // delete front } // end if } // end dequeue

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver void Queue::getFront(QueueItemType& queueFront) const { if (isEmpty()) cout << "QueueException: empty queue, cannot getFront"); else // queue is not empty; retrieve front queueFront = frontPtr->item; } // end getFront // End of implementation file.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver An Array-Based Implementation Figure 7-8 a) A naive array-based implementation of a queue; (b) rightward drift can cause the queue to appear full

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver An Array-Based Implementation A circular array –Eliminates the problem of rightward drift –BUT front and back cannot be used to distinguish between queue-full and queue- empty conditions

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver An Array-Based Implementation Figure 7-11 (a) front passes back when the queue becomes empty; (b) back catches up to front when the queue becomes full

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver An Array-Based Implementation To detect queue-full and queue-empty conditions –Keep a count of the queue items To initialize the queue, set –front to 0 –back to MAX_QUEUE – 1 –count to 0

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver An Array-Based Implementation Inserting into a queue back = (back+1) % MAX_QUEUE; items[back] = newItem; ++count; Deleting from a queue front = (front+1) % MAX_QUEUE; --count;

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver An Array-Based Implementation Variations of the array-based implementation 1.Use a flag isFull to distinguish between the full and empty conditions 2.Declare MAX_QUEUE + 1 locations for the array items, but use only MAX_QUEUE of them for queue items

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver An Array-Based Implementation Figure 7-12 A more efficient circular implementation: (a) a full queue; (b) an empty queue

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver QueueA.h */ #include "QueueException.h" const int MAX_QUEUE = maximum-size-of-queue; typedef desired-type-of-queue-item QueueItemType; * ADT queue - Array-based implementation. */ class Queue { public: Queue(); bool isEmpty() const; void enqueue(const QueueItemType& newItem) void dequeue(); void dequeue(QueueItemType& queueFront); void getFront(QueueItemType& queueFront) const ;

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver private: QueueItemType items[MAX_QUEUE]; int front; int back; int count; }; // end Queue // End of header file.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver QueueA.cpp * Circular array-based implementation. * The array has indexes to the front and back of the * queue. A counter tracks the number of items currently * in the queue. */ #include "QueueA.h" // header file Queue::Queue() : front(0), back(MAX_QUEUE-1), count(0) { } // end default constructor bool Queue::isEmpty() const { return count == 0; } // end isEmpty

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver void Queue::enqueue(const QueueItemType& newItem) { if (count == MAX_QUEUE) cout << “queue is full”; else { // queue is not full; insert item back = (back+1) % MAX_QUEUE; items[back] = newItem; ++count; } // end if } // end enqueue

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver void Queue::dequeue() { if (isEmpty()) cout << "QueueException: empty queue,” << “ cannot dequeue"); else { // queue is not empty; remove front front = (front+1) % MAX_QUEUE; --count; } // end if } // end dequeue

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver void Queue::dequeue(QueueItemType& queueFront) { if (isEmpty()) cout << "QueueException: empty queue, “ << “cannot dequeue"); else { // queue is not empty; retrieve and remove front queueFront = items[front]; front = (front+1) % MAX_QUEUE; --count; } // end if } // end dequeue

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver void Queue::getFront(QueueItemType& queueFront) const { if (isEmpty()) cout << "QueueException: empty queue,” << “ cannot getFront"); else // queue is not empty; retrieve front queueFront = items[front]; } // end getFront // End of implementation file.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver The Standard Template Library Class queue Some operations in the STL queue –Enqueue and dequeue operations are called push and pop, respectively, as for a stack –The back method returns a reference to the last item –The size method returns the number of items An adaptor container –Implemented using a more basic container type –The default queue container class is deque

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Comparing Implementations Fixed size versus dynamic size –A statically allocated array-based implementation Fixed-size queue that can get full Prevents the enqueue operation from adding an item to the queue, if the array is full –A dynamically allocated array-based implementation or a pointer-based implementation No size restriction on the queue

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Comparing Implementations A pointer-based implementation vs. one that uses a pointer-based implementation of the ADT list –Pointer-based implementation is more efficient –ADT list approach reuses an already implemented class Much simpler to write Saves programming time