【数据结构】队列基本操作的实现(C语言)(2)



> 
> 入队时判断当队列为空时`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)
//{
// size++;
// cur = cur->next;
//}
//return size;
return pq->size;
}
void TeseQueue()
{

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

PNF-1714273953248)]
[外链图片转存中…(img-Lb4Krkzw-1714273953248)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

  • 14
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值