//当队列不为空时
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
pq->size++;
}
>
> 入队时判断当队列为空时`pq->head = pq->tail = NULL`,不能对`pq->tail`解引用。
>
>
>
### 4.出队
void QueuePop(Queue* pq)//出队列
{
assert(pq);
assert(!QueueEmpty(pq)); //判断队列是否为空
//当队列中只剩一个结点时
if (pq->head->next == NULL)
{
free(pq->head);//释放节点
pq->head = pq->tail = NULL;//队列置空
}
//队列中有多个结点时
else
{
QNode* cur = pq->head;//记录下一个节点
pq->head = cur->next;
free(cur);//释放头节点
}
pq->size–;//有效个数–
}
>
> 出队时要判断队列是否为空,`assert(!QueueEmpty(pq))`。
>
>
>
### 5.打印
void QueuePrint(Queue* pq)//打印
{
assert(pq);
QNode* cur = pq->head;
while (cur)
{
printf(“%d “, cur->val);
cur = cur->next;
}
printf(”\n”);
}
>
> 注意循环条件`while (cur)`,不能写为`while(cur!=pq->tail)`。
>
>
>
### 6.返回队头的值
QDataType QueueFront(Queue* pq)//返回队头的值
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->head->val;
}
### 7.返回队尾的值
QDataType QueueBack(Queue* pq)//返回队尾的值
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->tail->val;
}
### 8.统计队列中有效值个数
int QueueSize(Queue* pq)//返回队列中有效值个数
{
assert(pq);
//int size = 0;
//QNode* cur = pq->head;
//while (cur)
//{
// size++;
// cur = cur->next;
//}
//return size;
return pq->size;
}
### 9.销毁
void QueueDestroy(Queue* pq)//销毁
{
assert(pq);
QNode* cur = pq->head;
//遍历,一个一个的释放空间
while (cur)
{
QNode* del = cur->next;
free(cur);
cur = del;
}
pq->head = pq->tail = NULL;//置空
}
## 三、测试代码
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <assert.h>
typedef int QDataType;
typedef struct QueueNode
{
QDataType val;
struct QueueNode* next;
}QNode;
typedef struct Queue {
QNode* head;
QNode* tail;
int size;
}Queue;
void QueueInit(Queue* pq)//初始化
{
assert(pq);
pq->head = pq->tail = NULL;
pq->size = 0;
}
void QueueDestroy(Queue* pq)//销毁
{
assert(pq);
QNode* cur = pq->head;
while (cur)
{
QNode* del = cur->next;
free(cur);
cur = del;
}
pq->head = pq->tail = NULL;
}
void QueuePrint(Queue* pq)//打印
{
assert(pq);
QNode* cur = pq->head;
while (cur)
{
printf(“%d “, cur->val);
cur = cur->next;
}
printf(”\n”);
}
bool QueueEmpty(Queue* pq)//判断是否为空
{
assert(pq);
return pq->head == NULL && pq->tail == NULL;
}
void QueuePush(Queue* pq, QDataType x)//入队
{
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror(“malloc”);
exit(-1);
}
newnode->val = x;
newnode->next = NULL;
if (pq->head == NULL)
{
pq->head = pq->tail = newnode;
}
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
pq->size++;
}
void QueuePop(Queue* pq)//出队列
{
assert(pq);
assert(!QueueEmpty(pq));
if (pq->head->next == NULL)
{
free(pq->head);
pq->head = pq->tail = NULL;
}
else
{
QNode* cur = pq->head;
pq->head = cur->next;
free(cur);
}
pq->size–;
}
QDataType QueueFront(Queue* pq)//返回队头的值
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->head->val;
}
QDataType QueueBack(Queue* pq)//返回队尾的值
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->tail->val;
}
int QueueSize(Queue* pq)//返回队列中有效值个数
{
assert(pq);
//int size = 0;
//QNode* cur = pq->head;
//while (cur)
//{
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
中…(img-cVzfEVcq-1714273987075)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!