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,

Slides:



Advertisements
Similar presentations
Ceng-112 Data Structures I Chapter 5 Queues.
Advertisements

Queues.
CHAPTER 7 Queues.
Data Structure Dr. Mohamed Khafagy.
COMPSCI 105 SS 2015 Principles of Computer Science Queues.
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
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.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
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.
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.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
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)
Stacks and Queues Introduction to Computing Science and Programming I.
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.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Tables and Priority Queues.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 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.
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
EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.
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)
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.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
April 27, 2017 COSC Data Structures I Review & Final Exam
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.
Queues Lecture 4. Lecture Objectives  Learn about queues  Examine various queue operations  Learn how to implement a queue as an array  Learn how.
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.
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
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.
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++
CC 215 Data Structures Queue ADT
Chapter 15 Lists Objectives
Csc 2720 Data structure Instructor: Zhuojun Duan
CENG 213 Data Structure Queue 7/2/2018.
The Abstract Data Type Queue
Queues Chapter 4.
Chapter 13 Queues and Priority Queues
Queues.
Queue and Priority Queue Implementations
Chapter 14: Queue and Priority Queue Implementations
CSC 143 Queues [Chapter 7].
Ch. 8 Queue Stack Queue milk queue stack
Figure 7.1 Some queue operations. Figure 7.1 Some queue operations.
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
Ch. 8 Queue Stack Queue milk queue stack
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.
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues.
Presentation transcript:

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

© 2004 Pearson Addison-Wesley. All rights reserved7 A-3 The Abstract Data Type Queue ADT queue operations –Create an empty queue –Determine whether a queue is empty –Add a new item to the queue –Remove from the queue the item that was added earliest –Remove all the items from the queue –Retrieve from the queue the item that was added earliest

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

© 2004 Pearson Addison-Wesley. All rights reserved7 A-5 The Abstract Data Type Queue Pseudocode for the ADT queue operations createQueue() // Creates an empty queue. isEmpty() // Determines whether a queue is empty enqueue(newItem) throws QueueException // Adds newItem at the back of a queue. Throws // QueueException if the operation is not // successful

© 2004 Pearson Addison-Wesley. All rights reserved7 A-6 The Abstract Data Type Queue Pseudocode for the ADT queue operations (Continued) dequeue() throws QueueException // Retrieves and removes the front of a queue. // Throws QueueException if the operation is // not successful. dequeueAll() // Removes all items from a queue peek() throws QueueException // Retrieves the front of a queue. Throws // QueueException if the retrieval is not // successful

© 2004 Pearson Addison-Wesley. All rights reserved7 A-7 The Abstract Data Type Queue Figure 7.1 Some queue operations

© 2004 Pearson Addison-Wesley. All rights reserved7 A-8 Simple Applications of the ADT Queue: Reading a String of Characters A queue can retain characters in the order in which they are typed queue.createQueue() while (not end of line) { Read a new character ch queue.enqueue(ch) } Once the characters are in a queue, the system can process them as necessary

© 2004 Pearson Addison-Wesley. All rights reserved7 A-9 // convert digits in queue into a decimal integer n // get first digit, ignoring any leading blanks do { ch = queue.dequeue() } while (ch is blank) // assertion: ch contains first digit // compute n from digits in queue n = 0 done = false do { n = 10 * n + integer that n represents if (!queue.is Empty()) { ch = queue.dequeue() } else { done = true } // end if } while (!done and ch is a digit) // Assertion: n is result

© 2004 Pearson Addison-Wesley. All rights reserved7 A-10 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 can be used to reverse the order of occurrences –A queue can be used to preserve the order of occurrences

© 2004 Pearson Addison-Wesley. All rights reserved7 A-11 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 Figure 7.2 The results of inserting a string into both a queue and a stack

© 2004 Pearson Addison-Wesley. All rights reserved7 A-12 isPal(str) // Determine whether str is a palindrome // create an empty queue and an empty stack queue.createQueue() stack.createStack() // insert each character of the string into both // the queue and stack length = the length of str for (i=1 through length) { nextChar = ith character of str queue.enqueue(nextChar) stack.push(nextChar) } // end for // compare the queue characters with the stack characters charactersAreEqual = true while (queue is not empty and charactersAreEqual is true) { queueFront = queue.dequeue() stackTop = stack.pop() if (queueFront not equal to stackTop) { charactersAreEqual = false } // end if } // end while return charactersAreEqual

© 2004 Pearson Addison-Wesley. All rights reserved7 A-13 Implementations of the ADT Queue A queue can have either –An array-based implementation –A reference-based implementation

© 2004 Pearson Addison-Wesley. All rights reserved7 A-14 A Reference-Based Implementation Possible implementations of a queue –A linear linked list with two external references A reference to the front A reference to the back Figure 7.3a A reference-based implementation of a queue: a) a linear linked list with two external references

© 2004 Pearson Addison-Wesley. All rights reserved7 A-15 A Reference-Based Implementation Possible implementations of a queue (Continued) –A circular linked list with one external reference A reference to the back Figure 7.3b A reference-based implementation of a queue: b) a circular linear linked list with one external reference

© 2004 Pearson Addison-Wesley. All rights reserved7 A-16 A Reference-Based Implementation Figure 7.4 Inserting an item into a nonempty queue

© 2004 Pearson Addison-Wesley. All rights reserved7 A-17 A Reference-Based Implementation Figure 7.5 Inserting an item into an empty queue: a) before insertion; b) after insertion

© 2004 Pearson Addison-Wesley. All rights reserved7 A-18 A Reference-Based Implementation Figure 7.6 Deleting an item from a queue of more than one item

© 2004 Pearson Addison-Wesley. All rights reserved7 A-19

© 2004 Pearson Addison-Wesley. All rights reserved7 A-20

© 2004 Pearson Addison-Wesley. All rights reserved7 A-21

© 2004 Pearson Addison-Wesley. All rights reserved7 A-22

© 2004 Pearson Addison-Wesley. All rights reserved7 A-23 An Array-Based Implementation Figure 7.7 a) A naive array-based implementation of a queue; b) rightward drift can cause the queue to appear full

© 2004 Pearson Addison-Wesley. All rights reserved7 A-24 An Array-Based Implementation A circular array eliminates the problem of rightward drift Figure 7.8 A circular implementation of a queue

© 2004 Pearson Addison-Wesley. All rights reserved7 A-25 An Array-Based Implementation Figure 7.9 The effect of some operations of the queue in Figure 7-8

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

© 2004 Pearson Addison-Wesley. All rights reserved7 A-27 An Array-Based Implementation Figure 7.10a a) front passes back when the queue becomes empty

© 2004 Pearson Addison-Wesley. All rights reserved7 A-28 An Array-Based Implementation Figure 7.10b b) back catches up to front when the queue becomes full

© 2004 Pearson Addison-Wesley. All rights reserved7 A-29 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

© 2004 Pearson Addison-Wesley. All rights reserved7 A-30 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;

© 2004 Pearson Addison-Wesley. All rights reserved7 A-31 An Array-Based Implementation Variations of the array-based implementation –Use a flag full 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

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

© 2004 Pearson Addison-Wesley. All rights reserved7 A-33 Please open file carrano_ppt07_B.ppt to continue viewing chapter 1.