+ All Categories
Home > Documents > Chapter 7 Queues Introduction Queue applications Implementations.

Chapter 7 Queues Introduction Queue applications Implementations.

Date post: 17-Jan-2018
Category:
Upload: blaise-malone
View: 240 times
Download: 0 times
Share this document with a friend
Description:
ADT of Generic Queue Constructor Constructor public Queue ( ) /* initialize an empty queue */ Inquiry into the queue Inquiry into the queue public boolean isEmpty ( ) public boolean isEmpty ( ) /* Determine whether this queue is empty */ /* Determine whether this queue is empty */ public int size( ) public int size( ) /* return the current number of items in this queue */ /* return the current number of items in this queue */
22
Chapter 7 Queues Chapter 7 Queues Introduction Introduction Queue Queue applications applications Implementations Implementations
Transcript
Page 1: Chapter 7 Queues Introduction Queue applications Implementations.

Chapter 7 QueuesChapter 7 Queues

• IntroductionIntroduction• Queue applicationsQueue applications• ImplementationsImplementations

Page 2: Chapter 7 Queues Introduction Queue applications Implementations.

Ch. 7 QueuesCh. 7 Queues

• A queue is a First-In/First-Out linear data A queue is a First-In/First-Out linear data structure: a data item is inserted into the rear or structure: a data item is inserted into the rear or taken out of the front of a sequence of elements, taken out of the front of a sequence of elements, which means that they are removed (dequeued) in which means that they are removed (dequeued) in the same order as they are inserted (enqueued)the same order as they are inserted (enqueued)

• The queue ADT for one data type can essentially The queue ADT for one data type can essentially be the same for another data type, other than the be the same for another data type, other than the type differences in method arguments and return type differences in method arguments and return valuesvalues

Page 3: Chapter 7 Queues Introduction Queue applications Implementations.

ADT of Generic QueueADT of Generic Queue

• ConstructorConstructor public Queue ( ) /* initialize an empty queue */

• Inquiry into the queueInquiry into the queue public boolean isEmpty ( )public boolean isEmpty ( ) /* Determine whether this queue is empty *//* Determine whether this queue is empty */

public int size( )public int size( ) /* return the current number of items in this queue *//* return the current number of items in this queue */

Page 4: Chapter 7 Queues Introduction Queue applications Implementations.

ADT of ObjectQueueADT of ObjectQueue• Adding an item at rearAdding an item at rear public void add (E item) /* add the given item as the last item of this queue */

• Removing (and getting) an item from frontRemoving (and getting) an item from front public E remove ( ) /* remove and return the first item of this queue */

Page 5: Chapter 7 Queues Introduction Queue applications Implementations.

Think about using a queueThink about using a queue

• The First-in/First-out and no-storage-restriction The First-in/First-out and no-storage-restriction properties of a queue makes it a good candidate properties of a queue makes it a good candidate data structure for implementing the kind of data structure for implementing the kind of processing where data come in sequence and need processing where data come in sequence and need to be stored until certain conditions are met and to be stored until certain conditions are met and then taken out to process, and where earlier data then taken out to process, and where earlier data has priorityhas priority

Page 6: Chapter 7 Queues Introduction Queue applications Implementations.

Example - checking palindromesExample - checking palindromes

• A palindrome is a string that reads forward and A palindrome is a string that reads forward and backward the same. To check if a string is a backward the same. To check if a string is a palindrome, compare the string with the reversed palindrome, compare the string with the reversed string, character by characterstring, character by character

E T T E

Page 7: Chapter 7 Queues Introduction Queue applications Implementations.

Example - checking palindromesExample - checking palindromes

TT

E

E

Page 8: Chapter 7 Queues Introduction Queue applications Implementations.

Example - checking palindromesExample - checking palindromes

T

T

E

E

E ET T

Page 9: Chapter 7 Queues Introduction Queue applications Implementations.

Example - car wash simulationExample - car wash simulation

• The method carWashSimulate is the control center of a car The method carWashSimulate is the control center of a car washwash

• An object of BooleanSource class is the random and An object of BooleanSource class is the random and probabilistic arrival of cars probabilistic arrival of cars

• An object of IntQueue class is the car waiting lineAn object of IntQueue class is the car waiting line• An object of Averager class is the bookkeeper (tracking: An object of Averager class is the bookkeeper (tracking:

number of cars, waiting time of a car, average waiting-number of cars, waiting time of a car, average waiting-time calculation; it is similar to, but simpler than, time calculation; it is similar to, but simpler than, Statictician)Statictician)

• An object of Washer class is the washing machineAn object of Washer class is the washing machine

Page 10: Chapter 7 Queues Introduction Queue applications Implementations.

Example - car wash simulationExample - car wash simulation

• In the method carWashSimulate: a loop cycles In the method carWashSimulate: a loop cycles through every second of the simulation time, through every second of the simulation time, coordinating possible arrival of a car, sending the car coordinating possible arrival of a car, sending the car to waiting queue, checking if the current car wash to waiting queue, checking if the current car wash process has finished and if so taking another car from process has finished and if so taking another car from queue to wash, and updating car waiting statistics queue to wash, and updating car waiting statistics

• In the BooleanSource object: each second of In the BooleanSource object: each second of simulation, a car comes or does not, randomly, but simulation, a car comes or does not, randomly, but with a probability that can be specified in advance with a probability that can be specified in advance

Page 11: Chapter 7 Queues Introduction Queue applications Implementations.

Example - car wash simulationExample - car wash simulation• In the Queue object: car arrive-time numbers enter at the In the Queue object: car arrive-time numbers enter at the

rear and leave from the frontrear and leave from the front• In the Averager object: whenever a car is taken to wash In the Averager object: whenever a car is taken to wash

this object is notified to update its record of car count, this object is notified to update its record of car count, and also update with the waiting time of this car for the and also update with the waiting time of this car for the purpose of average waiting time calculationpurpose of average waiting time calculation

• In the Washer object: when it is not busy it can be In the Washer object: when it is not busy it can be started, and then it runs busy for a pre-set amount of started, and then it runs busy for a pre-set amount of time (with a manual clock device), and then becomes time (with a manual clock device), and then becomes idle againidle again

Page 12: Chapter 7 Queues Introduction Queue applications Implementations.

Example - car wash simulationExample - car wash simulation

Car Wash: Control

Boolean Source:

Car Arrival

Queue:Car

Waiting

Averager:Car Waiting

Statistics

Washer:Car

Washing

Page 13: Chapter 7 Queues Introduction Queue applications Implementations.

Array implementation of a queueArray implementation of a queue

• Some difficulty with use of consecutive array Some difficulty with use of consecutive array positions when implementing a queue: the positions when implementing a queue: the currently-in-use section gradually shifts. leaving currently-in-use section gradually shifts. leaving empty space unused on one end but filling up the empty space unused on one end but filling up the other endother end

Car 3 Car 4 Car 5

Car 6Car 1Car 2

Page 14: Chapter 7 Queues Introduction Queue applications Implementations.

Circular arrayCircular array• A circular array is an array whose positions are A circular array is an array whose positions are

used in a circular manner: when the rear end fills used in a circular manner: when the rear end fills up but the front end has vacant positions the data up but the front end has vacant positions the data will continue (wrap-around) to fill from the first will continue (wrap-around) to fill from the first position on, again; and two index variables are position on, again; and two index variables are used to point to the front of the actual queue and used to point to the front of the actual queue and the rear of the actual queuethe rear of the actual queue

Car 8 Car 9 Car 5 Car 6 Car 7

FrontRear

Page 15: Chapter 7 Queues Introduction Queue applications Implementations.

Queue ADT Invariant for array Queue ADT Invariant for array implementationimplementation

• The current number of items in the queue is stored The current number of items in the queue is stored in the instance variable manyItemsin the instance variable manyItems

• For a non-empty queue, the items are stored in an For a non-empty queue, the items are stored in an instance variable circular array data, beginning at instance variable circular array data, beginning at data[front] and continuing through data[rear]; both data[front] and continuing through data[rear]; both front and rear are instance variablesfront and rear are instance variables

• For an empty queue, manyItems is 0, the data For an empty queue, manyItems is 0, the data array does not contain legitimate items, and so array does not contain legitimate items, and so there is no rule what front and rear must bethere is no rule what front and rear must be

Page 16: Chapter 7 Queues Introduction Queue applications Implementations.

Method to calculate circular array position for Method to calculate circular array position for next queue elementnext queue element

• This method takes an index integer and returns the This method takes an index integer and returns the next position index in the circular implementation of a next position index in the circular implementation of a queue queue

private int nextIndex (int currentPosition) { if (++currentPosition == data.length ) return 0; else return currentPosition; }

Already incremented

Page 17: Chapter 7 Queues Introduction Queue applications Implementations.

Using circular index - the getFront method of Using circular index - the getFront method of Generic ArrayQueueGeneric ArrayQueue

public E remove ( ) { if (manyItems == 0) throw new NoSuchElementException ( “Queue underflow.”); E answer = data[front]; front = nextIndex (front); manyItems --; return answer; }

Page 18: Chapter 7 Queues Introduction Queue applications Implementations.

Using circular index - the insert method of Using circular index - the insert method of ObjectQueueObjectQueue

public void add (E item) { if (manyItems == data.length) ensureCapacity (manyItems * 2 + 1); if (manyItems == 0) { front = 0; rear = 0; } else rear = nextIndex (rear); data[rear] = item; manyItems ++; }

Page 19: Chapter 7 Queues Introduction Queue applications Implementations.

The ensureCapacity method of The ensureCapacity method of ArrayQueueArrayQueue

Car 4Car 5Car 1Car 2Car 3

rear front

Car 1Car 2Car 3Car 4Car 5

rearfront

Page 20: Chapter 7 Queues Introduction Queue applications Implementations.

Linked list implementation of a queueLinked list implementation of a queue• Invariant of linked list implementation of the queue ADTInvariant of linked list implementation of the queue ADT– The number of items in the queue is stored in the instance The number of items in the queue is stored in the instance

variable manyItemsvariable manyItems– The items in the queue is stored in a linked list, with the The items in the queue is stored in a linked list, with the

first (head) node in instance variable front and last (tail) first (head) node in instance variable front and last (tail) node in instance variable rearnode in instance variable rear

– When the queue is empty, there is no linked list and both When the queue is empty, there is no linked list and both front and rear contain the null referencefront and rear contain the null reference

• Changes to the linked list occur only through adding at tail Changes to the linked list occur only through adding at tail or removing at heador removing at head

Page 21: Chapter 7 Queues Introduction Queue applications Implementations.

Priority queuePriority queue

• A priority queue is a queue-like data structure A priority queue is a queue-like data structure where each item also has a priority tag, where each item also has a priority tag, independent of when an item enters the queue independent of when an item enters the queue relative to other items in queuerelative to other items in queue

– A item of highest priority will always be the item A item of highest priority will always be the item taken out from the queuetaken out from the queue

– If there are several items of same highest priority, If there are several items of same highest priority, the one that enters the queue earliest will be taken the one that enters the queue earliest will be taken outout

Page 22: Chapter 7 Queues Introduction Queue applications Implementations.

Implementation of priority queueImplementation of priority queue

• An implementation of priority queueAn implementation of priority queue– In an instance variable is a data array of ArrayQueue In an instance variable is a data array of ArrayQueue

type (or LinkedLinkedQueue type)type (or LinkedLinkedQueue type)– Each of the array position has a (ordinary) queue in Each of the array position has a (ordinary) queue in

itself containing items of a same priorityitself containing items of a same priority– The array positions are for priorities 0, 1, 2, …, The array positions are for priorities 0, 1, 2, …,

highesthighest– A totalSize instance variable is used to keep track of A totalSize instance variable is used to keep track of

all the queue sizes togetherall the queue sizes together


Recommended