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

[数据结构]6. 队列-Queue

队列-Queue

  • 1. 介绍
  • 2. 队列实现
    • 2.1 基于链表的实现
    • 2.2 基于数组的实现
  • 3. 队列操作
    • Create
    • Initialize
    • Destory
    • Push
    • Pop
    • Front
    • Back
    • Size
    • Empty

1. 介绍

队列(queue) 是一种遵循先入先出规则的线性数据结构。将队列头部称为“队首”,尾部称为“队尾”,将把元素加入队尾的操作称为“入队”,删除队首元素的操作称为“出队”。
请添加图片描述

2. 队列实现

2.1 基于链表的实现

请添加图片描述请添加图片描述
请添加图片描述

2.2 基于数组的实现

请添加图片描述请添加图片描述
请添加图片描述

3. 队列操作

Create

typedef int QDataType;
typedef struct QueueNode
{struct QueueNode* next;QDataType data;
}QNode;typedef struct Queue
{QNode* phead;QNode* ptail;int size;
}Queue;

Initialize

void QueueInit(Queue* pq) {assert(pq);pq->phead = NULL;pq->ptail = NULL;pq->size = 0;
}

Destory

void QueueDestory(Queue* pq) {assert(pq);QNode* cur = pq->phead;while (cur) {QNode* next = cur->next;free(cur);cur = next;}pq->phead = pq->ptail = NULL;pq->size = 0;
}

Push

void QueuePush(Queue* pq, QDataType x) {assert(pq);QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL) {perror("malloc fail\n");return;}newnode->data = x;newnode->next = NULL;if (pq->ptail == NULL) {assert(pq->phead == NULL);pq->phead = pq->ptail = newnode;}else {pq->ptail->next = newnode;pq->ptail = newnode;}pq->size++;
}

Pop

void QueuePop(Queue* pq) {assert(pq);assert(!QueueEmpty(pq));// one nodeif (pq->phead->next == NULL) {free(pq->phead);pq->phead = pq->ptail = NULL;}// more nodeelse {QNode* next = pq->phead->next;free(pq->phead);pq->phead = next;}pq->size--;
}

Front

QDataType QueueFront(Queue* pq) {assert(pq);assert(!QueueEmpty(pq));return pq->phead->data;
}

Back

QDataType QueueBack(Queue* pq) {assert(pq);assert(!QueueEmpty(pq));return pq->ptail->data;
}

Size

int QueueSize(Queue* pq) {assert(pq);return pq->size;
}

Empty

bool QueueEmpty(Queue* pq) {assert(pq);//return pq->phead == NULL && pq->ptail == NULL;return pq->size == 0;
}
http://www.xdnf.cn/news/437113.html

相关文章:

  • 笛卡尔路径规划
  • React 第三十九节 React Router 中的 unstable_usePrompt Hook的详细用法及案例
  • React 第四十节 React Router 中 useBeforeUnload的使用详细解析及案例说明
  • Jsp技术入门指南【十四】实现基于MySQL+JDBC+JSP数据库验证的登录界面与登录跳转功能
  • LeRobot 框架的核心架构概念和组件(上)
  • 阿里端到端多模态语音对话开源模型论文速读:Qwen2.5-Omni
  • 机器学习 Day16 聚类算法 ,数据降维
  • SpringBoot整合MQTT实战:基于EMQX构建高可靠物联网通信,从零到一实现设备云端双向对话
  • ubuntu 22.04 wifi网卡配置地址上网
  • 关于深度学习的一些模型算法
  • AR禁毒:科技赋能,筑牢防毒新防线
  • 【MySQL】变更缓冲区:作用、主要配置以及如何查看
  • 前端服务器部署分类总结
  • 【工具类】ssh,remote-ssh插件和sftp插件
  • 【Oracle专栏】扩容导致数据文件 dbf 丢失,实操
  • LeetCode_sql刷题(3482.分析组织层级)
  • 鸿蒙OSUniApp制作一个小巧的图片浏览器#三方框架 #Uniapp
  • STM32 之网口资源
  • 大模型微调实战:基于GpuGeek平台的低成本高效训练方案
  • 光流 | Matlab工具中的光流算法
  • 团结引擎开源车模 Sample 发布:光照渲染优化 动态交互全面体验升级
  • MySQL八股(自用)
  • 学习threejs,使用Physijs物理引擎,通过控制重力,实现多米诺骨牌效果
  • Python+Selenium爬虫:豆瓣登录反反爬策略解析
  • Myshell与清华联合开源TTS模型OpenVoiceV2,多语言支持,风格控制进一步增强~
  • 深度学习入门:卷积神经网络
  • CSS常用选择器
  • Flink SQL 将kafka topic的数据写到另外一个topic里面
  • RPM 包制作备查 SRPM 包编译
  • 通过MCP让LLM调用系统接口