当前位置:网站首页>队列的链式存储
队列的链式存储
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;
}

链式存储不考虑队满的情况
边栏推荐
- Istio、eBPF 和 RSocket Broker:深入研究服务网格
- Chen Gong: Micro service, is it still so pure?
- Binary stack (I) - principle and C implementation
- Rural guys earn from more than 2000 a month to hundreds of thousands a year. Most brick movers can walk my way ǃ
- Typora realizes automatic uploading of picture pasting
- NOV Schedule for . Net to display and organize appointments and recurring events
- Joint Time-Frequency and Time Domain Learning for Speech Enhancement
- 我在中山,到哪里开户比较好?实际上网上开户安全么?
- 耐克如何常年霸榜第一名?最新財報答案來了
- CPU 上下文切换的机制和类型 (CPU Context Switch)
猜你喜欢

Theoretical basis of graph

消息队列之监控退款任务批处理过程

双链表有关操作

使用set_handler过滤掉特定的SystemC Wraning &Error Message

91. (cesium chapter) cesium rocket launch simulation

Exposure: a white box photo post processing framework reading notes

Self organization is the two-way rush of managers and members
![[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 8](/img/16/e1a0a52964c8a55eb729469114fc60.jpg)
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 8

Typora adds watermarks to automatically uploaded pictures

About keil compiler, "file has been changed outside the editor, reload?" Solutions for
随机推荐
Understanding of MVVM and MVC
NOV Schedule for . Net to display and organize appointments and recurring events
Golang introduces the implementation method of the corresponding configuration file according to the parameters
C summary of knowledge points 3
Seckill system 03 - redis cache and distributed lock
Machine learning - Data Science Library Day 3 - Notes
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 7
[classic example] classic list questions @ list
Talk about the pessimistic strategy that triggers full GC?
如何看懂开发的查询语句
Implementation of address book management system with C language
Custom grpc plug-in
On recursion and Fibonacci sequence
自定义 grpc 插件
Powerful, easy-to-use, professional editor / notebook software suitable for programmers / software developers, comprehensive evaluation and comprehensive recommendation
Explore the contour detection function findcontours() of OpenCV in detail with practical examples, and thoroughly understand the real role and meaning of each parameter and mode
陈珙:微服务,它还那么纯粹吗?
顺序表有关操作
Extended tree (I) - concept and C implementation
Use of easyexcel