g src="queue-circular-1.gif" alt="A Circular List Implementation of a Queue">
In this list, each item points to the next, and this chain of pointers eventually forms a loop back to the first one. With this structure, we need only one pointer to the tail of the queue, and, from this last element, the next field specifies the location of the head of the queue. This requirement of only one pointer allows the declarations to be simplified considerably, and a queue variable can take the place of this pointer to the last item.
/* Maximum length of names */
#define strMax 20
typedef struct node
{ char data [strMax];
struct node * next;
} queueNode;
typedef queueNode * stringQueue;
stringQueue queue;
This implementation illustrates another commonly used list structure, called a circular list, in which the last list item points back to the first one.
Such structures have the advantage that once processing starts within the list, there is no possibility of encountering a NULL pointer; all next fields always specify existing list items. On the other hand, the insertion of a first queue item requires a little care, as does the deletion of an item that is the only one contained in a queue. Thus, while a circular list does allow for a simplified implementation of queues, this circular structure still requires some special cases for queues with no elements or with just one element.
This document is available on the World Wide Web as
http://www.cs.grinnell.edu/~walker/courses/161.fa09/readings/reading-queues-circular.shtml
|
created 17 April 2008 last revised 4 May 2008 |
|
| For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |