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

【数据结构】链式队列

链式队列实现:

1.创建一个空队列

2.尾插法入队

3.头删法出队

4.遍历队列

一、main函数

 #include <stdio.h>                                                #include "./3.linkqueue.h"                                        int main(int argc, const char *argv[])                            {                                                                 linkpos* pos = create_linkqueue();                            show_linkqueue(pos);insertENd_linkqueue(pos,111);                                 insertENd_linkqueue(pos,222);                                 insertENd_linkqueue(pos,333);                                 insertENd_linkqueue(pos,444);                                 insertENd_linkqueue(pos,555);                                 show_linkqueue(pos);                                          dataType num = output_linkqueue(pos);                         printf("出队的数据为:%d\n",num);                             show_linkqueue(pos);                                          return 0;                                                     }                                                                 

二、功能函数

#include <stdio.h>                                                    
#include <stdlib.h>                                                   
#include "./3.linkqueue.h"                                            //创建                                                                
linkpos* create_linkqueue()                                           
{                                                                     linkpos* pos = (linkpos*)malloc(sizeof(linkpos));                 pos->front = (linkqueue*)malloc(sizeof(linkqueue));               if(NULL == pos->front)                                            {                                                                 printf("队列创建失败\n");                                     return NULL;                                                  }                                                                 pos->front->next =NULL;                                           pos->rear = pos->front;                                           pos->front->text.len = 0;                                         return pos;                                                       
}                                                                     //判空                                                                
int isEmpty_linkqueue(linkpos*pos)                                    
{                                                                     return pos->front == pos->rear?1:0;                               
}                                                                     //遍历                                                                
void show_linkqueue(linkpos*pos)                                      
{                                                                     if(isEmpty_linkqueue(pos))                                        {                                                                 printf("队列为空!\n");                                       return ;                                                      }                                                                 linkqueue* p = pos->front->next;                                  while(p != pos->rear)                                             {                                                                 printf("%d ",p->text.data);                                   p=p->next;                                                    }                                                                 printf("\n");                                                     return ;                                                          
}                                                                     //尾插 入队                                                           
void insertENd_linkqueue(linkpos*pos,dataType num)                    
{                                                                     linkqueue* temp = (linkqueue*)malloc(sizeof(linkqueue));          if(NULL == temp)                                                  {                                                                 printf("结点创建失败,入队失败\n");                           return;                                                       }                                                                 temp->next = NULL;                                                temp->text.data = num;                                            temp->next = pos->rear->next;                                     pos->rear->next = temp;                                           pos->rear = pos->rear->next;                                      pos->front->text.len++;                                           return;                                                           }                                                                     //头删 出队                                        
dataType output_linkqueue(linkpos*pos)             
{                                                  if(isEmpty_linkqueue(pos))                     {                                              printf("队列为空,无法出队\n");            return (dataType)-1;                       }                                              linkqueue* temp;                               temp = pos->front->next;                       pos->front->next = temp->next;                 dataType num =temp->text.data; if(pos->front->next == NULL) {pos->rear = pos->front;}               free(temp);                                    return num;                                    
}                                                  

三、头文件

 #ifndef __LINKQUEUE_H__#define __LINKQUEUE_H__typedef int dataType;union msg{dataType data;int len;};typedef struct node{union msg text;struct node* next;}linkqueue;typedef struct{linkqueue* front;linkqueue* rear;}linkpos;linkpos* create_linkqueue();void insertENd_linkqueue(linkpos*pos,dataType num);dataType output_linkqueue(linkpos*pos);void show_linkqueue(linkpos*pos);                           #endif

四、运行结果

相关文章:

  • FlinkSql一个简单的测试程序
  • Docker中部署flink集群的两种方式
  • SQL字符集
  • Web 前端 UI 框架Bootstrap简介与基本使用
  • 手拉手Vite+Vue3+TinyVue+Echarts+TailwindCSS
  • 武汉AAA企业信用等级认证
  • 【MATLAB】 EWT信号分解+FFT傅里叶频谱变换组合算法
  • 【DAY03 软考中级备考笔记】存储系统,总线系统,输入输出系统和可靠性
  • verilog学习
  • vue 使用docx库生成word表格文档
  • 编程笔记 Golang基础 013 格式化输入输出
  • 企业级SAS盘SSDPM1643a PM1653 Nytro 2050 KPM71VUG3T20固态硬盘
  • Rust-知多少?
  • mysql-多表查询-内连接
  • java日志框架总结(六、logback日志框架 扩展)
  • C++11: atomic 头文件
  • canvas 五子棋游戏
  • JavaScript设计模式之工厂模式
  • JSONP原理
  • learning koa2.x
  • Python 使用 Tornado 框架实现 WebHook 自动部署 Git 项目
  • ReactNativeweexDeviceOne对比
  • 阿里云容器服务区块链解决方案全新升级 支持Hyperledger Fabric v1.1
  • 从地狱到天堂,Node 回调向 async/await 转变
  • 对象引论
  • 分类模型——Logistics Regression
  • 计算机常识 - 收藏集 - 掘金
  • 通过git安装npm私有模块
  • 容器镜像
  • ​​快速排序(四)——挖坑法,前后指针法与非递归
  • (11)MATLAB PCA+SVM 人脸识别
  • (ISPRS,2023)深度语义-视觉对齐用于zero-shot遥感图像场景分类
  • (PHP)设置修改 Apache 文件根目录 (Document Root)(转帖)
  • (Redis使用系列) Springboot 使用redis实现接口幂等性拦截 十一
  • (独孤九剑)--文件系统
  • (分享)一个图片添加水印的小demo的页面,可自定义样式
  • (四)汇编语言——简单程序
  • (四)图像的%2线性拉伸
  • (完整代码)R语言中利用SVM-RFE机器学习算法筛选关键因子
  • .360、.halo勒索病毒的最新威胁:如何恢复您的数据?
  • .gitattributes 文件
  • .NET MVC第五章、模型绑定获取表单数据
  • .net(C#)中String.Format如何使用
  • .net用HTML开发怎么调试,如何使用ASP.NET MVC在调试中查看控制器生成的html?
  • .Net中wcf服务生成及调用
  • .vollhavhelp-V-XXXXXXXX勒索病毒的最新威胁:如何恢复您的数据?
  • /etc/X11/xorg.conf 文件被误改后进不了图形化界面
  • @private @protected @public
  • @RequestBody与@ResponseBody的使用
  • @transactional 方法执行完再commit_当@Transactional遇到@CacheEvict,你的代码是不是有bug!...
  • @Transactional 详解
  • [20180129]bash显示path环境变量.txt
  • [2021 蓝帽杯] One Pointer PHP
  • [C#]winform部署yolov5-onnx模型
  • [C语言]——分支和循环(4)