①单链表
typedef struct qnote {
ElemType data;
struct qnote* next;
}DataNote;
typedef struct {
DataNote* front;
DataNote* rear;
}LinkQuNote;
初始化
void InitQueue(LinkQuNote* q) { q = (LinkQuNote*)malloc(sizeof(LinkQuNote)); q->front = q->rear = NULL; }
销毁
void DestroyQueue(LinkQuNote*& q)
{
DataNote* pre = q->front->next,* p;
if (pre != NULL)
{
p = pre->next;
while (p != NULL)
{
free(pre);
pre = p;
p = pre->next;
}
free(pre);
}
free(q);
}
判断是否为空
bool QueueEmpty(LinkQuNote* q)
{
return(q->rear = NULL);
}
进队
void enQueue(LinkQuNote* q, ElemType e)
{
DataNote* p = (DataNote*)malloc(sizeof(DataNote));
p->data = e;
p->next = NULL;
if (q->rear = NULL)
{
q->front = q->rear = p;
}
else
{
q->rear->next = p;
q->rear = p;
}
}
出队
bool delQueue(LinkQuNote*& q, ElemType& e)
{
DataNote* t;
if (q->rear == NULL)
return false;
t = q->front;
if (q->front == q->rear)
q->front = q->rear = NULL;
else
{
q->front = q->front->next;
}
e = t->data;
free(t);
return true;
}
②循环单链表
typedef struct LNode {
ElemType data;
struct LNode* next;
}LinkNode;
初始化
void InitQueue(LinkNode*& rear)
{
rear = NULL;
}
判断是否为空
bool QueueEmpty(LinkNode* rear)
{
return (rear == NULL);
}
进队
void enQueue(LinkNode*& rear, ElemType e)
{
LinkNode* p = (LinkNode*)malloc(sizeof(LinkNode));
p->data = e;
if (rear == NULL)
{
p->next = p;
rear = p;
}
else
{
p->next = rear->next;
rear->next = p;
rear = p;
}
}
出队
bool delQueue(LinkNode*& rear, ElemType& e)
{
LinkNode* p;
if (rear == NULL)
return false;
else if (rear->next == rear)
{
e = rear->data;
free(rear);
rear = NULL;
}
else
{
p = rear->next;
e = p->data;
rear->next = p->next;
free(p);
}
return true;
}