# /* q.c */ /* EMACS_MODES: c !fill */ /* General-purpose queue manipulation routines. Contains the following * routines: * q_deq dequeue and return first element from queue * q_del delete element from queue * All other general-purpose queue manipulation routines are macros * defined in "q.h". */ #include "q.h" #define FALSE 0 #define TRUE 1 q_elt q_deq (q) /* Dequeue and return the first element of the specified queue. Returns * a pointer to the first element if any, or NULL if the queue is empty. * * Arguments: */ register queue *q; { register q_elt temp; /* temp for result */ if ((temp = q->q_head) == NULL) /* queue empty? */ return (NULL); /* yes, show none */ q->q_head = temp->qe_next; /* else unlink */ temp->qe_next = NULL; /* avoid dangling pointers */ if (q->q_head == NULL) /* queue empty? */ q->q_tail = NULL; /* yes, update tail pointer too */ q->q_len--; /* update queue length */ return (temp); } q_del (q, elt) /* Delete the specified element from the queue. This requires scanning * the queue from the top to find and remove the element, so it takes * O(queue length) time to execute. Note that this routine must not * run at interrupt level. * Returns TRUE if the element is successfully deleted, or FALSE if * the element is not found in the queue. */ register queue *q; /* the queue */ register q_elt elt; /* element to delete */ { register q_elt *tmp; /* temp for chaining */ for (tmp = &q->q_head; *tmp != elt; tmp = (q_elt *)(((q_elt)tmp)->qe_next)) if (*tmp == NULL) /* find ptr. in queue to elt. */ return (FALSE); /* if not in queue, punt */ *tmp = (*tmp)->qe_next; /* else link it out of the queue */ if (q->q_tail == elt) { /* at end of queue? */ if (tmp == &q->q_head) /* yes; if first elt, zero out tail */ q->q_tail = NULL; else /* otherwise tail is previous elt */ q->q_tail = (q_elt)tmp; } elt->qe_next = NULL; /* avoid dangling pointers */ q->q_len--; /* update element count */ return (TRUE); }