Queues |
|
The queue is an abstract data type that obeys a First In First Out (FIFO) rule. It is used where elements are processed in the order in which they arrive. As such it finds many uses in computer operating systems - for example the process queue, the print queue.
If queues do not share elements, procedures may be written to manipulate them as side-effects, much as for stacks. Frequently front and popq are combined in one procedure which returns the front elements and removes it from the queue. Example: Breadth-First Traversal of a TreeA queue is used in the algorithm to traverse a tree in breadth-first order. This is given in the chapter on binary trees. Queue by ArrayA queue can be implemented by an array of elements. Two integers point to the first and last elements respectively. Pointer arithmetic is done modulo the maximum size. For this reason it is more convenient if the array index runs from zero to the maximum size minus one.
This implementation places a limit on the number of elements and is often called a bounded queue. Queue by Circular ListThere are various ways of implementing a queue by a list. Since front and addq require fast access to opposite ends of the list, a pointer can be kept to each end of the list. A neat variation is to use a circular list where the queue variable points to the last element:
Exercises
© L. Allison
|
|
↑ © L. Allison, www.allisons.org/ll/ (or as otherwise indicated). Created with "vi (Linux)", charset=iso-8859-1, fetched Thursday, 01-Jun-2023 05:50:00 UTC. Free: Linux, Ubuntu operating-sys, OpenOffice office-suite, The GIMP ~photoshop, Firefox web-browser, FlashBlock flash on/off. |