123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- /* queue.c -- the Queue type implementation*/
- #include <stdio.h>
- #include <stdlib.h>
- #include "queue.h"
- /* local functions */
- static void CopyToNode(Item item, Node * pn);
- static void CopyToItem(Node * pn, Item * pi);
- void InitializeQueue(Queue * pq)
- {
- pq->front = pq->rear = NULL;
- pq->items = 0;
- }
- bool QueueIsFull(const Queue * pq)
- {
- return pq->items == MAXQUEUE;
- }
- bool QueueIsEmpty(const Queue * pq)
- {
- return pq->items == 0;
- }
- int QueueItemCount(const Queue * pq)
- {
- return pq->items;
- }
- bool EnQueue(Item item, Queue * pq)
- {
- Node * pnew;
-
- if (QueueIsFull(pq))
- return false;
- pnew = (Node *) malloc( sizeof(Node));
- if (pnew == NULL)
- {
- fprintf(stderr,"Unable to allocate memory!\n");
- exit(1);
- }
- CopyToNode(item, pnew);
- pnew->next = NULL;
- if (QueueIsEmpty(pq))
- pq->front = pnew; /* item goes to front */
- else
- pq->rear->next = pnew; /* link at end of queue */
- pq->rear = pnew; /* record location of end */
- pq->items++; /* one more item in queue */
-
- return true;
- }
- bool DeQueue(Item * pitem, Queue * pq)
- {
- Node * pt;
-
- if (QueueIsEmpty(pq))
- return false;
- CopyToItem(pq->front, pitem);
- pt = pq->front;
- pq->front = pq->front->next;
- free(pt);
- pq->items--;
- if (pq->items == 0)
- pq->rear = NULL;
-
- return true;
- }
- /* empty the queue */
- void EmptyTheQueue(Queue * pq)
- {
- Item dummy;
- while (!QueueIsEmpty(pq))
- DeQueue(&dummy, pq);
- }
- /* Local functions */
- static void CopyToNode(Item item, Node * pn)
- {
- pn->item = item;
- }
- static void CopyToItem(Node * pn, Item * pi)
- {
- *pi = pn->item;
- }
|