home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Media Share 9
/
MEDIASHARE_09.ISO
/
progmisc
/
ad12.zip
/
QUEUE.AD
< prev
next >
Wrap
Text File
|
1990-02-27
|
2KB
|
68 lines
┌ * Queue
│ * Implemenation of a queue as a linked data structure
│ *
│ * Operations supported:
│ * enqueue(x) place item x at the end of the queue
│ * head return the item at the head of the queue
│ * dequeue remove next item from the front of the queue
│ * empty return true iff the queue is empty
│ *
│ ┌ * Data structures
│ │ * qHead pointer to the head of the queue
│ │ * qTail pointer to the tail of the queue
│ │ * qItem queue member structure
│ │ * next pointer to next item on queue
│ │ * anItem the item itself - must be of proper type
│ │ * to contain the type of items on the queue
│ │
│ │ typedef long member; * storing longs on this queue
│ │ typedef struct item item;
│ │
│ │ struct item {
│ │ item *next;
│ │ member anItem;
│ │ };
│ │ item *quhead = NULL, *qtail = NULL;
│ └
│
│ ┌ * Functions
│ │
│ │ * add an item to queue
│ │ ┌ void enqueue(item x) {
│ │ │ * Allocate a new item structure
│ │ │ item *new; * pointer to new node
│ │ │ new = malloc(sizeof item);
│ │ │
│ │ │ * Place the item on the queue
│ │ │ new->item = x;
│ │ │ new->next = NULL;
│ │ │
│ │ │ ╒ If (empty())
│ │ │ │ {
│ │ │ │ * Queue is empty, point the queue head and tail at the new item
│ │ │ │ qHead = new;
│ │ │ │ qTail = new;
│ │ │ │ }
│ │ │ ├ Else
│ │ │ │ {
│ │ │ │ * Queue is not empty, insert the item and point everything to it
│ │ │ │ qTail->next = new;
│ │ │ │ qTail = new;
│ │ │ │ }
│ │ │ └
│ │ │
│ │ └ }
│ │
│ │ * remove next item from queue
│ │ ┌ void dequeue() { │ │ │ * head is always next item │ │ │ qHead = qHead->next; │ │ │ │ │ │ ╒ If (qHead == NULL) │ │ │ │ * queue is now empty │ │ │ │ qTail = NULL; │ │ │ └ │ │ │ │ │ └ }
│ │
│ │ * return item at head of queue; don't remove
│ │ ┌ member head() { │ │ │ return (qHead->anItem); │ │ └ }
│ │
│ │ * return true if the queue is empty, false otherwise
│ │ ┌ int empty() { │ │ │ return (qHead == NULL); │ │ └ }
│ │
│ └
│
└