#include #include #include "c:queue.h" /* initialize queue by setting both front and rear to NULL */ void CreateQueue(Q_TYPE *queue) { queue->front=NULL; queue->rear=NULL; } /* resets queue to empty & returns the elements of the queue to the system */ void DestroyQueue(Q_TYPE *queue) { NODE_PTR temp_ptr; while ( EmptyQueue(queue) == FALSE) { temp_ptr=queue->front; queue->front=queue->front->next; free(temp_ptr); } queue->rear = NULL; } /* returns true if front is NULL */ BOOLEAN EmptyQueue(Q_TYPE *queue) { if (queue->front == NULL) return TRUE; else return FALSE; } /* always returns false */ BOOLEAN FullQueue() { NODE_PTR test_ptr = (NODE_PTR) malloc(sizeof(NODE_TYPE)); if (test_ptr==NULL) return TRUE; else { free(test_ptr); return FALSE; } } /* adds an element to the rear of the queue */ void Enqueue(Q_TYPE *queue, ITEM_TYPE new_item) { NODE_PTR new_node; new_node=(NODE_PTR) malloc(sizeof(NODE_TYPE)); new_node->item=new_item; new_node->next=NULL; if (EmptyQueue(queue)==TRUE) queue->front=new_node; else /* if the queue is not empty */ queue->rear->next=new_node; queue->rear=new_node; } /* removes an element from the front of the queue */ void Dequeue(Q_TYPE *queue, ITEM_TYPE *front_item) { NODE_PTR temp_ptr; *front_item=queue->front->item; temp_ptr=queue->front; queue->front=queue->front->next; if (EmptyQueue(queue)) queue->rear=NULL; free(temp_ptr); }