当前位置:网站首页>队列的链式存储
队列的链式存储
2022-07-01 12:05:00 【Between the steps】
队列的链式存储
1.初始化
#include<stdio.h>
typedef struct LinkNode{
ElemType data;
struct LinkNode *next;
}LinkNode;
typedef struct{
LinkNode *front,*rear;
}LinkQueue;
//初始化队列,带头结点
void InitQueue(LinkQueue &Q){
//初始化时,front 和rear 都指向头结点
Q.front=Q.rear=(LinkNode*)malloc(sizeof(LinkNode));
Q.front->next=NULL;
}
void testLinkQueue(){
LinkQueue Q; //声明一个队列
InitQueue(Q); //初始化队列
//后续操作
}

2.是否为空
bool isEmpty(LinkQueue Q){
if(Q.rear==Q.front)
return true;
else
return false;
}
3.不带头结点初始化队列
//初始化队列,不带头结点
void InitQueue(LinkQueue &Q){
//初始化时,front 和rear 都指向NULL
Q.front=NULL;
Q.rear=NULL;
}
4.不带头结点判空
bool isEmpty(LinkQueue Q){
if(Q.front==NULL)
return true;
else
return false;
}
5.入队操作(带头结点) 类似于尾插法
void EnQueue(LinkQueue &Q,ElemType x){
LinkNode *s=(LinkNode*)malloc(sizeof(LinkNode));
s.data=x;
s->next=NULL;
Q.rear->next=s; //新结点插入rear之后
Q.rear=s; //修改表尾指针
}

6.入队不带头结点 (可能里面有元素 可能空元素插入 )
//新元素入队 不带头结点
void EnQueue(LinkQueue &Q,ElemType x){
LinkNode *s=(LinkNode*)malloc(sizeof(LinkNode)); //开辟空间存放新元素
s->data=x; //插入的值放进新开辟的空间
s->next=NULL; //新开辟空间指向NULL
if(Q.front==NULL){
//说明没有元素 也没有头结点 只有Q.front 和Q.rear 指针
Q.front=s;
Q.rear=s;
}
else{
Q.rear->next=s;
Q.rear=s;
}
}

7.出队操作(带头结点)
bool DeQueue(LinkQueue &Q,ElemType &x){
if(Q.front==Q.rear) //说明是空栈
return false;
LinkNode *p=Q.front->next; //p指向第一个结点
x=p->data; //用变量x返回队头元素
Q.front->next=p->next; //修改头结点的next指针
if(Q.rear==p){
//最后一个结点出栈
Q.rear=Q.front; //修改rear指针
}
free(p);
return true;
}


8.不带头结点的出队操作
bool DeQueue(LinkQueue &Q,ElemType &x){
if(Q.front==NULL) //说明是空栈
return false;
LinkNode *p=Q.front; //p指向出队的结点
x=p->data; //变量x返回头元素
Q.front=p->next; //修改front指针
if(Q.rear==p){
//最后一个结点出栈
Q.front=NULL;
Q.rear=NULL;
}
free(p);
return true;
}

链式存储不考虑队满的情况
边栏推荐
- Sum of factor numbers of interval product -- prefix sum idea + fixed one shift two
- Understanding of MVVM and MVC
- Dlhsoft Kanban, Kanban component of WPF
- Learning summary on June 28, 2022
- Self organization is the two-way rush of managers and members
- Unity xlua co process packaging
- Istio, ebpf and rsocket Broker: in depth study of service grid
- 伸展树(一) - 概念和C实现
- LeetCode 454. 四数相加 II
- 【20211129】Jupyter Notebook远程服务器配置
猜你喜欢

S7-1500plc simulation

Dlhsoft Kanban, Kanban component of WPF

研发效能度量框架解读

ABBIRB120工业机器人机械零点位置

Uniapp uses uni upgrade Center

S7-1500PLC仿真

GID:旷视提出全方位的检测模型知识蒸馏 | CVPR 2021

Onenet Internet of things platform - mqtt product equipment upload data points
![[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 7](/img/41/e3ecbd49e4bfeab6c6e7d8733fe33a.jpg)
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 7

消息队列之监控退款任务批处理过程
随机推荐
Extended tree (I) - concept and C implementation
NOV Schedule for . Net to display and organize appointments and recurring events
指纹浏览器工作原理、使用场景以及重要性简单讲解
91. (cesium chapter) cesium rocket launch simulation
C#依赖注入(直白明了)讲解 一看就会系列
[106] 360 check font - check whether the copyright of local Fonts is commercially available
ACLY与代谢性疾病
Adjacency matrix undirected graph (I) - basic concepts and C language
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 6
队列操作---
LeetCode 454. Add four numbers II
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 7
Comment Nike a - t - il dominé la première place toute l'année? Voici les derniers résultats financiers.
Botu V15 add GSD file
C # dependency injection (straight to the point) will be explained as soon as you see the series
图的理论基础
Virtualenv+pipenv virtual environment management
GID:旷视提出全方位的检测模型知识蒸馏 | CVPR 2021
Seckill system 03 - redis cache and distributed lock
Building external modules