#include "linkQueue.h"
status initQueue(linkQueuePtr Q)
{
(*Q).front = (*Q).rear = (queuePtr) malloc(sizeof(qNode));
if((*Q).front == NULL)
{
printf("malloc error in initQueue\n");
return ERROR;
}
(*Q).front->next = NULL;
return OK;
}
status enQueue(linkQueuePtr Q, qElemType e)
{
queuePtr p;
p = (queuePtr) malloc(sizeof(qNode));
if(!p)
{
printf("malloc error in enQueue\n");
return ERROR;
}
p->data = e;
p->next = NULL;
(*Q).rear->next = p;
(*Q).rear = p;
return OK;
}
status deQueue(linkQueuePtr Q, qElemType *e)
{
queuePtr p;
if(Q->front == Q->rear)
{
printf("an empty queue\n");
return ERROR;
}
p = Q->front->next;
(*e) = p->data;
Q->front->next = p->next;
if(p == Q->rear)
{
Q->rear = Q->front;
}
free(p);
return OK;
}
status destroyQueue(linkQueuePtr Q)
{
while(Q->front)
{
Q->rear = Q->front->next;
free(Q->front);
Q->front = Q->rear;
}
return OK;
}