Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queues Another Linear ADT Copyright © 2009 Curt Hill.

Similar presentations


Presentation on theme: "Queues Another Linear ADT Copyright © 2009 Curt Hill."— Presentation transcript:

1 Queues Another Linear ADT Copyright © 2009 Curt Hill

2 Introduction Bank and grocery store lines Unlike Stack, two places of action Adds are at one end –Rear or tail Deletes at the other –Head or front First In First Out Copyright © 2009 Curt Hill

3 Operations Create a queue Append a request for service Service a request Is the queue full? Is the queue empty? Queue size Copyright © 2009 Curt Hill

4 Queue of Integer In C++ class Queue{ public: bool enter(int); int remove(); bool remove(int &); bool isFull(); bool isEmpty(); int size(); private: … }; Copyright © 2009 Curt Hill

5 Potential Data Structures Straight array Circular array Files Linked list Copyright © 2009 Curt Hill

6 Straight array Two possibilities Move every element after a service –Inefficient for queue of any size Move head and tail –Only useful if queue lengths are short –And queue is regularly cleared Copyright © 2009 Curt Hill

7 Circular array Use modulo arithmetic Head or tail can wrap around from highest to lowest subscript This is classic OS buffer technique Copyright © 2009 Curt Hill

8 Circular array queues Declare the array to be MAX long Two indices –One points to where things enter - tail –Other where they are removed - head Each is incremented with something like this: index = ++index%MAX; –Circular increment Unless MAX is power of two it requires a division Copyright © 2009 Curt Hill

9 Questions What does the head and tail point at? –First unused –Last used Do we increment before using or after? Does not matter as long as consistent We also end up with the same problems Copyright © 2009 Curt Hill

10 Lets Assume … For the sake of presentation That head (item next to leave) points at an unused –Must be incremented first That tail (end of queue) points at an unused –Must be incremented after How should they be initialized for an empty queue Copyright © 2009 Curt Hill

11 Initialization Head and tail should be set to the same index –Does not matter which, so zero is fine An empty queue has the two the same How then do we signify a full queue? Two possibilities: –Waste a slot –Additional boolean Copyright © 2009 Curt Hill

12 The full queue If we always maintain one empty slot –Then full is (tail+1)%MAX == head IF additional boolean –Call it full and set to false initially –If tail catches head set full to true –Any call to remove sets to false –Most of the time boolean is false Linked queues do not have this problem Copyright © 2009 Curt Hill

13 Files Perhaps the worst unless is queue is enormous Open and close are inefficient Only really useful if queue length is very long And lots of appends followed by lots of serves resulting in clearing of queue Spooling is an example Copyright © 2009 Curt Hill

14 Linked list Either use a hand designed or STL list Add things on one end Remove from other For an STL list use push_back and pop_front as your pair or the reverse Copyright © 2009 Curt Hill

15 Linked queues The basic approach is similar to a linked stack What used to be the top of the stack is the head of the queue One additional pointer points to tail of the queue, which is the last item on the list When an item leaves the queue the code is essentially the same as pop except that the pointer has to be checked for NULL When an item enters the queue: –New item created –Pointer to last item updates the NULL in it to point to newly created item –Tail pointer then shifts down –But we must check that the tail pointer is not NULL, which we did not need to do with stack Copyright © 2009 Curt Hill

16 Problems with queues Queue, as described, have no notion of priority How should this be solved? Two possibilities –Multiple queues –Priority queue Copyright © 2009 Curt Hill

17 Multiple queues If there are few possible priorities Just use one queue for each value Always serve the first item on the highest non-empty queue An OS often has priority levels fewer than 16 and often less Copyright © 2009 Curt Hill

18 Priority queue The only thing needing change is the method that adds a new item to the queue It does not put it at the end but puts it just below the lowest item with same or higher priority A simple append now becomes a search for correct position Much easier for a queue made from a list Copyright © 2009 Curt Hill

19 Operating system example Any operating system is a good example because –There are resources –These resources are requested –The requests are queued –Some of the queues can be quite long CPU resource –Terminals waiting for service after an enter –Programs waiting for a time slice –Non-interactive programs waiting CPU time I/O subsystem –Programs waiting to do a disk read/write –Prints to be printed at one of several printers –Other output to be given (graph, punch, whatever) Copyright © 2009 Curt Hill

20 More OS examples Memory –Programs waiting a memory page –Memory pages waiting for use Other –Telecommunication devices waiting for service –Queue of letters to be read –Queue of system wide messages –Buffers of files are a queue Copyright © 2009 Curt Hill


Download ppt "Queues Another Linear ADT Copyright © 2009 Curt Hill."

Similar presentations


Ads by Google