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

C语言实现简单的--队列

 1,队列实现源码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>typedef struct node {int data;struct node *next;
}QNode;typedef struct qnode {int size;QNode *head;QNode *tail;
}Queue;Queue *init_queue()
{Queue *head = (Queue *)calloc(1, sizeof(Queue));if(head){head->size = 0;head->head = head->tail = NULL;return head;}return NULL;
}QNode *CreatNode(int dat)
{QNode *no = (QNode *)calloc(1, sizeof(QNode));if(no){no->data = dat;no->next = NULL;return no;}printf("%s: creat node is failed.\n", __FUNCTION__);return NULL;
}int QueEmpty(Queue *que)
{if(!que) {printf("%s: queue is NULL.\n", __FUNCTION__);return -1;}return que->size == 0;
}int QueSize(Queue *que)
{if(!que) {printf("%s: queue is NULL.\n", __FUNCTION__);return -1;}return que->size;
}int push_queue(Queue *que, QNode *element)
{if(!element || !que) {printf("%s : element/que is NULL.\n", __FUNCTION__);return -1;}if (!que->tail) {// queue is NULLque->head = que->tail = element;que->size++;} else {que->tail->next = element;que->tail = element;que->size++;}return 0;
}int pop_queue(Queue *que, int *data)
{//取出数据域if(!que) {printf("%s: queue is NULL.\n", __FUNCTION__);return -1;}if (!que->head) {printf("%s: queue->head is NULL.\n", __FUNCTION__);return -1;}else {QNode *tmp = que->head->next;*data = que->head->data;free(que->head);que->head = tmp;que->size--;if(QueEmpty(que))que->tail = NULL;}return 0;
}void QueDestroy(Queue *que) 
{if (QueEmpty(que)) {return ;}QNode *tmp = que->head;QNode *tp = NULL;while(tmp){tp = tmp; tmp = tmp->next;if(tp) {free(tp);printf("1\n");}}que->size = 0;que->tail = NULL;que->head = NULL;
}int main()
{int i = 0;int data = 0;Queue *que = init_queue();QNode *no = NULL;for(i = 0; i< 5; i++){no = CreatNode(i);if(no){push_queue(que, no);}}no = NULL;i = 0;QueDestroy(que);printf("------------size: %d--------------\n", QueSize(que));while(!QueEmpty(que)){if(!pop_queue(que, &data)) {printf("index: %d, data: %d\n", i, data);}i++;}printf("------------size: %d--------------\n", QueSize(que));printf("head addr: %p\n", que->head);printf("tail addr: %p\n", que->tail);free(que);return 0;
}

 2,总结

队列:

        ① 队列有head和tail,出队操作只能从头出,入队操作只能从tail入

        ② 一般的接口:

                创建队列,初始化队列,创建队内节点,入队操作,出队操作,队满,队空

        上面的代码并没有通过size限制队列的长度

http://www.xdnf.cn/news/475777.html

相关文章:

  • Redis解析
  • C#将1GB大图裁剪为8张图片
  • 100G QSFP28 BIDI光模块一览:100G单纤高速传输方案|易天光通信
  • 组件导航 (Navigation)+flutter项目搭建-混合开发+分栏
  • Android 中 权限分类及申请方式
  • HNU工训--计算机串口数据收发与测量
  • 安科瑞AcrelEMS3.0企业微电网智慧能源平台-安科瑞 蒋静
  • .NET Core liunx二进制文件安装
  • 22、能源监控与优化 - 数据中心模拟 - /能源管理组件/data-center-energy-monitoring
  • CSS面试题汇总
  • 中文分词与数据可视化02
  • 接触感知 钳位电路分析
  • [模型部署] 3. 性能优化
  • 我的 PDF 工具箱:CodeBuddy 打造 PDFMagician 的全过程记录
  • Java 并发编程归纳总结(可重入锁 | JMM | synchronized 实现原理)
  • 【LeetCode 热题 100】动态规划 系列
  • 从 Vue3 回望 Vue2:生命周期的清晰化——从混乱钩子到明确时机
  • 2025年渗透测试面试题总结-安恒[实习]安全服务工程师(题目+回答)
  • git克隆github项目到本地的三种方式
  • Vue百日学习计划Day16-18天详细计划-Gemini版
  • matlab建立整车模型,求汽车的平顺性
  • 【Golang笔记01】Goland基础语法规则
  • Leaflet 自定义瓦片地图与 PHP 大图切图算法 解决大图没办法在浏览器显示的问题
  • 容器编排利器-k8s入门指南
  • 移植RTOS,发现任务栈溢出怎么办?
  • 哪个品牌的智能对讲机好用?推荐1款,能扛事更智能
  • MySQL基础
  • Jenkins教程
  • 2025云智算技术白皮书
  • 青少年编程与数学 02-019 Rust 编程基础 16课题、包、单元包及模块