Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 Queues Introduction Queue applications Implementations.

Similar presentations


Presentation on theme: "Chapter 7 Queues Introduction Queue applications Implementations."— Presentation transcript:

1 Chapter 7 Queues Introduction Queue applications Implementations

2 Ch. 7 Queues A queue is a First-In/First-Out linear data structure: a data item is inserted into the rear or taken out of the front of a sequence of elements, which means that they are removed (dequeued) in the same order as they are inserted (enqueued) A queue is a First-In/First-Out linear data structure: a data item is inserted into the rear or taken out of the front of a sequence of elements, which means that they are removed (dequeued) in the same order as they are inserted (enqueued) The queue ADT for one data type can essentially be the same for another data type, other than the type differences in method arguments and return values The queue ADT for one data type can essentially be the same for another data type, other than the type differences in method arguments and return values

3 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 */

4 ADT of ObjectQueue Adding an item at rear Adding 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 front Removing (and getting) an item from front public E remove ( ) /* remove and return the first item of this queue */

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

6 Example - checking palindromes A palindrome is a string that reads forward and backward the same. To check if a string is a palindrome, compare the string with the reversed string, character by character A palindrome is a string that reads forward and backward the same. To check if a string is a palindrome, compare the string with the reversed string, character by character ETTE

7 Example - checking palindromes T T E E

8 T T E E EETT

9 Example - car wash simulation The method carWashSimulate is the control center of a car wash The method carWashSimulate is the control center of a car wash An object of BooleanSource class is the random and probabilistic arrival of cars An object of BooleanSource class is the random and probabilistic arrival of cars An object of IntQueue class is the car waiting line An object of IntQueue class is the car waiting line An object of Averager class is the bookkeeper (tracking: number of cars, waiting time of a car, average waiting-time calculation; it is similar to, but simpler than, Statictician) An object of Averager class is the bookkeeper (tracking: number of cars, waiting time of a car, average waiting-time calculation; it is similar to, but simpler than, Statictician) An object of Washer class is the washing machine An object of Washer class is the washing machine

10 Example - car wash simulation In the method carWashSimulate: a loop cycles through every second of the simulation time, coordinating possible arrival of a car, sending the car to waiting queue, checking if the current car wash process has finished and if so taking another car from queue to wash, and updating car waiting statistics In the method carWashSimulate: a loop cycles through every second of the simulation time, coordinating possible arrival of a car, sending the car to waiting queue, checking if the current car wash process has finished and if so taking another car from queue to wash, and updating car waiting statistics In the BooleanSource object: each second of simulation, a car comes or does not, randomly, but with a probability that can be specified in advance In the BooleanSource object: each second of simulation, a car comes or does not, randomly, but with a probability that can be specified in advance

11 Example - car wash simulation In the Queue object: car arrive-time numbers enter at the rear and leave from the front In the Queue object: car arrive-time numbers enter at the rear and leave from the front In the Averager object: whenever a car is taken to wash this object is notified to update its record of car count, and also update with the waiting time of this car for the purpose of average waiting time calculation In the Averager object: whenever a car is taken to wash this object is notified to update its record of car count, and also update with the waiting time of this car for the purpose of average waiting time calculation In the Washer object: when it is not busy it can be started, and then it runs busy for a pre-set amount of time (with a manual clock device), and then becomes idle again In the Washer object: when it is not busy it can be started, and then it runs busy for a pre-set amount of time (with a manual clock device), and then becomes idle again

12 Example - car wash simulation Car Wash: Control Boolean Source: Car Arrival Queue: Car Waiting Averager: Car Waiting Statistics Washer: Car Washing

13 Array implementation of a queue Some difficulty with use of consecutive array positions when implementing a queue: the currently-in-use section gradually shifts. leaving empty space unused on one end but filling up the other end Some difficulty with use of consecutive array positions when implementing a queue: the currently-in-use section gradually shifts. leaving empty space unused on one end but filling up the other end Car 3Car 4Car 5 Car 6 Car 1 Car 2

14 Circular array A circular array is an array whose positions are used in a circular manner: when the rear end fills up but the front end has vacant positions the data will continue (wrap-around) to fill from the first position on, again; and two index variables are used to point to the front of the actual queue and the rear of the actual queue A circular array is an array whose positions are used in a circular manner: when the rear end fills up but the front end has vacant positions the data will continue (wrap-around) to fill from the first position on, again; and two index variables are used to point to the front of the actual queue and the rear of the actual queue Car 8Car 9Car 5Car 6Car 7 FrontRear

15 Queue ADT Invariant for array implementation The current number of items in the queue is stored in the instance variable manyItems The current number of items in the queue is stored in the instance variable manyItems For a non-empty queue, the items are stored in an instance variable circular array data, beginning at data[front] and continuing through data[rear]; both front and rear are instance variables For a non-empty queue, the items are stored in an instance variable circular array data, beginning at data[front] and continuing through data[rear]; both front and rear are instance variables For an empty queue, manyItems is 0, the data array does not contain legitimate items, and so there is no rule what front and rear must be For an empty queue, manyItems is 0, the data array does not contain legitimate items, and so there is no rule what front and rear must be

16 Method to calculate circular array position for next queue element This method takes an index integer and returns the next position index in the circular implementation of a queue This method takes an index integer and returns the next position index in the circular implementation of a queue private int nextIndex (int currentPosition) { if (++currentPosition == data.length ) return 0; else return currentPosition; } Already incremented

17 Using circular index - the getFront method of Generic ArrayQueue public E remove ( ) { if (manyItems == 0) throw new NoSuchElementException ( “Queue underflow.”); E answer = data[front]; front = nextIndex (front); manyItems --; return answer; }

18 Using circular index - the insert method of ObjectQueue 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 ++; }

19 The ensureCapacity method of ArrayQueue Car 4Car 5Car 1Car 2Car 3 rear front Car 1Car 2Car 3Car 4Car 5 rearfront

20 Linked list implementation of a queue Invariant of linked list implementation of the queue ADT Invariant of linked list implementation of the queue ADT – The number of items in the queue is stored in the instance variable manyItems – The items in the queue is stored in a linked list, with the first (head) node in instance variable front and last (tail) node in instance variable rear – When the queue is empty, there is no linked list and both front and rear contain the null reference Changes to the linked list occur only through adding at tail or removing at head Changes to the linked list occur only through adding at tail or removing at head

21 Priority queue A priority queue is a queue-like data structure where each item also has a priority tag, independent of when an item enters the queue relative to other items in queue A priority queue is a queue-like data structure where each item also has a priority tag, independent of when an item enters the queue relative to other items in queue – A item of highest priority will always be the item taken out from the queue – If there are several items of same highest priority, the one that enters the queue earliest will be taken out

22 Implementation of priority queue An implementation of priority queue An implementation of priority queue – In an instance variable is a data array of ArrayQueue type (or LinkedLinkedQueue type) – Each of the array position has a (ordinary) queue in itself containing items of a same priority – The array positions are for priorities 0, 1, 2, …, highest – A totalSize instance variable is used to keep track of all the queue sizes together


Download ppt "Chapter 7 Queues Introduction Queue applications Implementations."

Similar presentations


Ads by Google