当前位置:网站首页>队列(c语言/链表)
队列(c语言/链表)
2022-04-23 07:42:00 【AuroraN】
用链表储存队列,原理是放两个头指针和尾指针,在队尾添加元素,在队首删除元素,清楚这个原理,再考虑一下特殊情况即可,比方说删除元素后队列内无元素及时把头指针尾指针指向空,其余的情况看代码即可
#include<stdio.h>
#include<windows.h>
typedef struct LinkLNode
{
int data;
struct LinkLNode* next;
}LinkLNode;
typedef struct
{
LinkLNode* rear;
LinkLNode* front;
int item;
}LinkQueue;
void Init(LinkQueue &Q){
LinkLNode* s;
s=(LinkLNode*)malloc(sizeof(LinkLNode));
Q.front=NULL;
Q.rear=NULL;
Q.item=0;
}
bool IsEmpty(LinkQueue Q){
if(Q.front==NULL)
return true;
else
return false;
}
void EnQueue(LinkQueue &Q,int n){
LinkLNode* s;
s=(LinkLNode*)malloc(sizeof(LinkLNode));
s->data=n;
s->next=NULL;
if(IsEmpty(Q)){
Q.front=s;
}else{
Q.rear->next=s;
}
Q.rear=s;
Q.item++;
}
void DeQueue(LinkQueue &Q){
if(IsEmpty(Q)){
return;
}
LinkLNode* s;
s=Q.front;
Q.front=Q.front->next;
free(s);
if(Q.item==0){
Q.front=NULL;
Q.rear=NULL;
}
}
void printQueue(LinkQueue Q){
LinkLNode* i=(LinkLNode*)malloc(sizeof(LinkLNode));
i=Q.front;
while(i!=NULL){
printf("%d\n",i->data);
i=i->next;
}
}
int main()
{
LinkQueue Q;
Init(Q);
int n;
int a[10];
scanf("%d",&n);
for (int i = 0; i < n; i++)
{
scanf("%d",&a[i]);
EnQueue(Q,a[i]);
}
printQueue(Q);
printf("***\n");
DeQueue(Q);
printQueue(Q);
system("pause");
return 0;
}
版权声明
本文为[AuroraN]所创,转载请带上原文链接,感谢
https://blog.csdn.net/AuroraN/article/details/124354199
边栏推荐
- C outputs a two-dimensional array with the following characteristics.
- Campus transfer second-hand market source code download
- 数论求a^b(a,b为1e12级别)的因子之和
- 室内定位技术对比
- 基于TCP/IP协议的网络通信实例——文件传输
- How to encrypt devices under the interconnection of all things
- 跨域配置报错: When allowCredentials is true, allowedOrigins cannot contain the special value “*“
- The annotation is self-defined by implementing the parameter parser handlermethodargumentresolver interface
- 对OutputStream类的flush()方法的误解
- 社区团购小程序源码+界面diy+附近团长+供应商+拼团+菜谱+秒杀+预售+配送+直播
猜你喜欢
An article understands variable lifting
WordPress爱导航主题 1.1.3 简约大气网站导航源码网址导航源码
谈谈那些基础但不简单的股票数据
Qtablewidget header customization and beautification developed by pyqt5 (with source code download)
LeetCode简单题之统计字符串中的元音子字符串
利用Js实现一个千分位
WordPress love navigation theme 1.1.3 simple atmosphere website navigation source code website navigation source code
【学习】从零开始的音视频开发(9)——NuPlayer
[appium] encountered the problem of switching the H5 page embedded in the mobile phone during the test
Qt读写XML文件
随机推荐
数据可视化:使用Excel制作雷达图
为什么会存在1px问题?怎么解决?
Comparison of indoor positioning technology
一键清理项目下pycharm和Jupyter缓存文件
一个必看的微信小程序开发指南1-基础知识了解
freertos学习02-队列 stream buffer message buffer
Vowel substring in statistical string of leetcode simple problem
WordPress爱导航主题 1.1.3 简约大气网站导航源码网址导航源码
[C语言] 文件操作《一》
校园转转二手市场源码下载
有意思的js 代码
一款拥有漂亮外表的Typecho简洁主题_Scarfskin 源码下载
idea:使用easyYapi插件导出yapi接口
Weekly leetcode - 06 array topics 7 ~ 739 ~ 50 ~ offer 62 ~ 26 ~ 189 ~ 9
QFileDialog 选择多个文件或文件夹
Generate and parse tokens using JWT
5.6 comprehensive case - RTU-
js将树形结构数据转为一维数组数据
LeetCode简单题之统计字符串中的元音子字符串
华硕笔记本电脑重装系统后不能读取usb,不能上网