当前位置: 首页 > news >正文

手撕 队列

队列的基本概念

只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出

入队列:进行插入操作的一端称为队尾

出队列:进行删除操作的一端称为队头

队列用链表实现

队列的实现

队列的定义

队列初始化

入队

出队

判断队列是否为空

销毁

队头数据

队尾数据

队列数据

运行调试

完整代码

Queue.h

#pragma once#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>typedef int QDataType;typedef struct QueueNode
{struct QueueNode* next;QDataType data;
}QNode;typedef struct Queue
{QNode* head;QNode* tail;int size;
}Que;void QueueInit(Que* pst);     //初始化队列void QueuePush(Que* pst,QDataType x);     //入队void QueuePop(Que* pst);          //出队bool QueueEmpty(Que* pst);          //判断队列是否为空int QueueSize(Que* pst);      //队列数据QDataType QueuBack(Que* pst);      //队尾数据QDataType QueueFront(Que* pst);       //队头数据void QueueDestroy(Que* pst);        //销毁队列

Queue.c

#define _CRT_SECURE_NO_WARNINGS 1#include "Queue.h"void QueueInit(Que* pst)     //初始化队列
{assert(pst);pst->head = NULL;pst->tail = NULL;pst->size = 0;}void QueuePush(Que* pst, QDataType x)     //入队
{assert(pst);QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc failed");exit(-1);}newnode->data = x;newnode->next = NULL;if (pst->tail == NULL){pst->head = newnode;pst->tail = newnode;}else{pst->tail->next = newnode;pst->tail = newnode;}pst->size++;
}void QueuePop(Que* pst)          //出队
{//队列为空assert(pst);assert(!QueueEmpty(pst));//队列只有一个元素if (pst->head->next == NULL){free(pst->head);pst->head = pst->tail = NULL;}//队列多个元素else{QNode* del = pst->head;pst->head = pst->head->next;free(del);}pst->size--;
}bool QueueEmpty(Que* pst)          //判断队列是否为空
{assert(pst);return pst->head == NULL;
}void QueueDestroy(Que* pst)        //销毁队列
{assert(pst);QNode* cur = pst->head;while (cur){QNode* next = cur->next;free(cur);cur = next;}pst->head = NULL;pst->tail = NULL;pst->size = 0;
}QDataType QueueFront(Que* pst)        //队头数据
{assert(pst);assert(!QueueEmpty(pst));return pst->head->data;
}QDataType QueuBack(Que* pst)      //队尾数据
{assert(pst);assert(!QueueEmpty(pst));return pst->tail->data;
}int QueueSize(Que* pst)      //队列数据
{assert(pst);return pst->size;
}

http://www.lryc.cn/news/159665.html

相关文章:

  • 【autodl/linux配环境心得:conda/本地配cuda,cudnn及pytorch心得】-未完成
  • macOS Ventura 13.5.2(22G91)发布,附黑/白苹果镜像下载地址
  • vue 子组件向父组件传递参数 子传父
  • 自然语言处理学习笔记(八)———— 准确率
  • Matlab 如何选择窗函数和 FFT 的长度
  • node.js下载安装环境配置以及快速使用
  • 使用栈检查括号的合法性 C 实现
  • 小白备战大厂算法笔试(四)——哈希表
  • 云原生Kubernetes:pod基础
  • Ansys Zemax | 手机镜头设计 - 第 3 部分:使用 STAR 模块和 ZOS-API 进行 STOP 分析
  • CSP-J初赛复习大题整理笔记
  • 面试题 ⑤
  • 硅谷课堂1
  • 第6节-PhotoShop基础课程-认识选区
  • SQLServer如何获取客户端IP
  • 爬虫数据清洗可视化实战-就业形势分析
  • Python - 队列【queue】task_done()和join()基本使用
  • springboot web 增加不存在的url返回200状态码 vue 打包设置
  • JavaWeb_LeadNews_Day11-KafkaStream实现实时计算文章分数
  • python tcp server client示例代码
  • typecho 反序列化漏洞复现
  • Python实现SSA智能麻雀搜索算法优化LightGBM分类模型(LGBMClassifier算法)项目实战
  • Java多线程4种拒绝策略
  • MySQL的MHA
  • Java实现链表
  • SpringCloud Alibaba(2021.0.1版本)微服务-OpenFeign以及相关组件使用(保姆级教程)
  • 豆制品废水处理设备源头厂家方案
  • lnmp环境搭建
  • 全球研发中心城市专题协商会课题调研组莅临麒麟信安考察指导
  • ZeroTier客户端连接服务器