📄 queue.c
字号:
// queue.c
#include "queue.h"
#include "malloc.h"
#include "string.h"
bool InitQueue(LinkQueue *Q)
{
Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode));
if (!Q->front)
return false;
Q->front->next = NULL;
return true;
}
bool DestroyQueue(LinkQueue *Q)
{
while (Q->front)
{
Q->rear = Q->front->next;
free(Q->front);
Q->front = Q->rear;
}
return true;
}
bool ClearQueue(LinkQueue *Q)
{
DestroyQueue(Q);
return InitQueue(Q);
}
bool QueueEmpty(LinkQueue Q)
{
return (Q.front == Q.rear);
}
int QueueLength(LinkQueue Q)
{
return Q.rear-Q.front;
}
bool GetHead(LinkQueue Q, QElemType *pdata)
{
if (QueueEmpty(Q))return false;
*pdata = Q.front->next->data;
return true;
}
bool EnQueue(LinkQueue *Q, QElemType data)
{
QueuePtr p;
p = (QueuePtr)malloc(sizeof(QNode));
if (!p)
return false;
p->data = data;
p->next = NULL;
Q->rear->next = p;
Q->rear = p;
return true;
}
bool DeQueue(LinkQueue *Q, QElemType *pdata)
{
// 头结点
QueuePtr p = Q->front->next;
if (QueueEmpty(*Q))return false;
Q->front->next = p->next;
*pdata = p->data;
// 不要疏忽了这个可能
if (p==Q->rear)
Q->rear = Q->front;
free(p);
return true;
}
bool QueueTraverse(LinkQueue *Q, bool (*visit)(QElemType *))
{
// 头结点
QueuePtr p = Q->front->next;
while(p)
{
if (!visit(&p->data))
return false;
p = p->next;
}
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -