#ifndef _LINKQUEUE_H_
#define _LINKQUEUE_H_
struct LinkNode{
QElemType data;
LinkNode* next;
};
struct LinkQueue
{
LinkNode* front;
LinkNode* rear;
};
void InitQueue(LinkQueue&Q)
{
Q.front = Q.rear = new LinkNode;
Q.front->next = NULL;
}
void ClearQueue(LinkQueue&Q)
{
LinkNode*p;
while (Q.front->next != NULL)
{
p = Q.front->next;
Q.front->next = p->next;
delete p;
}
Q.rear = Q.front;
}
void DestroyQueue(LinkQueue& Q)
{
ClearQueue(Q);
delete Q.front;
Q.front = Q.rear = NULL;
}
bool QueueEmpty(LinkQueue Q)
{
return Q.front == Q.rear;
}
int QueueLength(LinkQueue Q)
{
int i = 0;
LinkNode* p = Q.front->next;
while (p != NULL)
{
i++;
p = p->next;
}
return i;
}
QElemType GetHead(LinkQueue& Q)
{
return Q.front->next->data;
}
QElemType GetLast(LinkQueue& Q)
{
return Q.rear->data;
}
void EnQueue(LinkQueue& Q, QElemType e)
{
LinkNode* p;
p = new LinkNode;
p->data = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
}
bool DeQueue(LinkQueue& Q, QElemType& e)
{
if (QueueEmpty(Q))
return false;
LinkNode* p = Q.front->next;
Q.front->next = p->next;
e = p->data;
if (p == Q.rear)
Q.rear = Q.front;
delete p;
return true;
}
#endif // !_LINKQUEUE_H_