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.

Slides:



Advertisements
Similar presentations
Queues.
Advertisements

Fundamentals of Python: From First Programs Through Data Structures
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
COMPSCI 105 SS 2015 Principles of Computer Science Queues.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
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.
Queues and Priority Queues
CS Data Structures I Chapter 7 Queues II. 2 Topics Queue Application Simulation Comparison of List, Stack and Queue.
COSC2006 Chapter 8 Queues III
Chapter 13 Queues and Priority Queues CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Circular Arrays Neat trick: use a circular array to insert and remove items from a queue in constant time The idea of a circular array is that the end.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Cmpt-225 Queues. A queue is a data structure that only allows items to be inserted at the end and removed from the front Queues are FIFO (First In First.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular.
1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Queues and Priority Queues
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues.
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’
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 7: Queues Data Abstraction & Problem Solving with C++
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
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)
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
UNIVERSAL COLLEGE OF ENGG. AND TECH. 3 RD IT. QUEUE ( data structure)
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.
Chapter 7 Queues Introduction Queue applications Implementations.
Linear Data Structures
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
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.
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++
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Chapter 7 Stacks © 2006 Pearson Addison-Wesley. All rights reserved 7A-1.
Chapter 6 A Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 A-2 The Abstract Data Type: Developing an ADT During the Design of a Solution.
Queues Chapter 8 (continued)
Review Array Array Elements Accessing array elements
Chapter 7 Queues.
Data Abstraction & Problem Solving with C++
Data Structures Using C++ 2E
CC 215 Data Structures Queue ADT
Csc 2720 Data structure Instructor: Zhuojun Duan
CENG 213 Data Structure Queue 7/2/2018.
Queues Queues Queues.
The Abstract Data Type Queue
Chapter 13 Queues and Priority Queues
Queues.
Queue and Priority Queue Implementations
Chapter 14: Queue and Priority Queue Implementations
CSC 143 Queues [Chapter 7].
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.
CSCS-200 Data Structure and Algorithms
Queues and Priority Queue Implementations
Queues Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues.
Presentation transcript:

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

© 2005 Pearson Addison-Wesley. All rights reserved7-3 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 A simulation –A study to see how to reduce the wait involved in an application

© 2005 Pearson Addison-Wesley. All rights reserved7-4 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

© 2005 Pearson Addison-Wesley. All rights reserved7-5 The Abstract Data Type Queue Definitions for the ADT queue operations +createQueue() +destroyQueue() +isEmpty():boolean {query} +enqueue(in newItem:QueueItemType)throw QueueException

© 2005 Pearson Addison-Wesley. All rights reserved7-6 The Abstract Data Type Queue Definitions for the ADT queue operations +dequeue() throw QueueException +dequeue(out queueFront:QueueItemType) throw QueueException +getFront(out queueFront:QueueItemType) throw QueueException

© 2005 Pearson Addison-Wesley. All rights reserved7-7 The Abstract Data Type Queue Figure 7.2 Some queue operations

© 2005 Pearson Addison-Wesley. All rights reserved7-8 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

© 2005 Pearson Addison-Wesley. All rights reserved7-9 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, a queue can be used in conjunction with a stack –A stack reverses the order of occurrences –A queue preserves the order of occurrences

© 2005 Pearson Addison-Wesley. All rights reserved7-10 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

© 2005 Pearson Addison-Wesley. All rights reserved7-11 Recognizing Palindromes Figure 7.3 The results of inserting a string into both a queue and a stack

© 2005 Pearson Addison-Wesley. All rights reserved7-12 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

© 2005 Pearson Addison-Wesley. All rights reserved7-13 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

© 2005 Pearson Addison-Wesley. All rights reserved7-14 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

© 2005 Pearson Addison-Wesley. All rights reserved7-15 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

© 2005 Pearson Addison-Wesley. All rights reserved7-16 An Array-Based Implementation A circular array eliminates the problem of rightward drift A problem with the circular array implementation –front and back cannot be used to distinguish between queue-full and queue-empty conditions

© 2005 Pearson Addison-Wesley. All rights reserved7-17 An Array-Based Implementation Figure 7.11b (a) front passes back when the queue becomes empty; (b) back catches up to front when the queue becomes full

© 2005 Pearson Addison-Wesley. All rights reserved7-18 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

© 2005 Pearson Addison-Wesley. All rights reserved7-19 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;

© 2005 Pearson Addison-Wesley. All rights reserved7-20 An Array-Based Implementation Variations of the array-based implementation –Use a flag isFull to distinguish between the full and empty conditions –Declare MAX_QUEUE + 1 locations for the array items, but use only MAX_QUEUE of them for queue items

© 2005 Pearson Addison-Wesley. All rights reserved7-21 An Array-Based Implementation Figure 7.12 A more efficient circular implementation: a) a full queue; b) an empty queue

© 2005 Pearson Addison-Wesley. All rights reserved7-22 An Implementation That Uses the ADT List If the item in position 1 of a list aList represents the front of the queue, the following implementations can be used –dequeue() aList.remove(1) –getFront(queueFront) aList.retrieve(1, queueFront)

© 2005 Pearson Addison-Wesley. All rights reserved7-23 An Implementation That Uses the ADT List If the item at the end of the list represents the back of the queue, the following implementations can be used –enqueue(newItem) aList.insert(getLength() + 1, newItem)

© 2005 Pearson Addison-Wesley. All rights reserved7-24 The Standard Template Library Class queue Some operations in the STL queue –Dequeue and enqueue operations are called push and pop, as in a stack –The back function returns a reference to the last item –The size function returns the number of items An adaptor container –Implemented using a more basic container type –The default queue container class is deque

© 2005 Pearson Addison-Wesley. All rights reserved7-25 Comparing Implementations Fixed size versus dynamic size –A statically allocated array Prevents the enqueue operation from adding an item to the queue if the array is full –A resizable array or a reference-based implementation Does not impose this restriction on the enqueue operation

© 2005 Pearson Addison-Wesley. All rights reserved7-26 Comparing Implementations Pointer-based implementations –A linked list implementation More efficient –The ADT list implementation Simpler to write

© 2005 Pearson Addison-Wesley. All rights reserved7-27 A Summary of Position-Oriented ADTs Position-oriented ADTs –List –Stack –Queue Stacks and queues –Only the end positions can be accessed Lists –All positions can be accessed

© 2005 Pearson Addison-Wesley. All rights reserved7-28 A Summary of Position-Oriented ADTs Stacks and queues are very similar –Operations of stacks and queues can be paired off as createStack and createQueue Stack isEmpty and queue isEmpty push and enqueue pop and dequeue Stack getTop and queue getFront

© 2005 Pearson Addison-Wesley. All rights reserved7-29 A Summary of Position-Oriented ADTs ADT list operations generalize stack and queue operations –getLength –insert –remove –retrieve

© 2005 Pearson Addison-Wesley. All rights reserved7-30 Application: Simulation Simulation –A technique for modeling the behavior of both natural and human-made systems –Goal Generate statistics that summarize the performance of an existing system Predict the performance of a proposed system –Example A simulation of the behavior of a bank

© 2005 Pearson Addison-Wesley. All rights reserved7-31 Application: Simulation Figure 7.14 A bank line at time a) 0; b) 12 c) 20; d) 38

© 2005 Pearson Addison-Wesley. All rights reserved7-32 Application: Simulation An event-driven simulation –Simulated time is advanced to time of the next event –Events are generated by a mathematical model that is based on statistics and probability

© 2005 Pearson Addison-Wesley. All rights reserved7-33 Application: Simulation A time-driven simulation –Simulated time is advanced by a single time unit –The time of an event is determined randomly and compared with a simulated clock

© 2005 Pearson Addison-Wesley. All rights reserved7-34 Application: Simulation The bank simulation is concerned with –Arrival events External events: the input file specifies the times at which the arrival events occur –Departure events Internal events: the simulation determines the times at which the departure events occur

© 2005 Pearson Addison-Wesley. All rights reserved7-35 Application: Simulation An event list is needed to implement an event-driven simulation –An event list Keeps track of arrival and departure events that will occur but have not occurred yet Contains at most one arrival event and one departure event

© 2005 Pearson Addison-Wesley. All rights reserved7-36 Summary The definition of the queue operations gives the ADT queue first-in, first-out (FIFO) behavior A pointer-based implementation of a queue uses either –A circular linked list –A linear linked list with a head reference and a tail reference

© 2005 Pearson Addison-Wesley. All rights reserved7-37 Summary A circular array eliminates the problem of rightward drift in an array-based implementation Distinguishing between the queue-full and queue-empty conditions in a circular array –Count the number of items in the queue –Use a isFull flag –Leave one array location empty

© 2005 Pearson Addison-Wesley. All rights reserved7-38 Summary Simulations –In a time-driven simulation Time is advanced by a single time unit –In an event-driven simulation Time is advanced to the time of the next event –To implement an event-driven simulation, you maintain an event list that contains events that have not yet occurred