当前位置:网站首页>队列操作---
队列操作---
2022-07-01 12:05:00 【Between the steps】
队列
队列,也是顺序存储结构,其基本操作:增删改查,判空,判满操作
1.初始化
#include<stdio.h>
#define Maxsize 10 //栈中最大元素个数
typedef struct{
ElemType data[Maxsize]; //静态数组存放队列元素
int front,rear; //队头指针和队尾指针
}SqQueue;
void InitQueue(SqQueue &Q){
//初始化 队头队尾指针指向0
Q.front=Q.rear=0;
}
void testQueue{
Queue Q; //声明一个队列,为其分配空间
//后续操作
InitStack(Q);
}
2.判空操作(队头指针等于队尾指针)
bool QueueEmpty(SqQueue Q){
if(Q.front==Q.rear)
return true; //队空
else
return false;
}
3.入队操作
bool EnQueue(SqQueue &Q,ElemType x){
if((Q.rear+1)%MaxSize==Q.front) //判断队满 并不是 Q.rear==Q.front ,必须牺牲一个单元
return false;
Q.data[Q.rear]=x; //将x插入队尾
Q.rear=(Q.rear+1)%Maxsize; //队尾指针后移
return true;
}
4.出队操作
bool DeQueue(SqQueue &Q,ElemType &x){
if(Q.rear==Q.front)
return false; //队空报错
x=Q.data[Q.front]; //取出删除的值
Q.front=(Q.front+1)%Maxsize; //队头指针改变
return true;
}
出队入队操作指针的变化一样
Q.rear=(Q.rear+1)%Maxsize
Q.front=(Q.front+1)%Maxsize
5.获得队头元素的值
bool GetHead(SqQueue &Q,ElemType &x){
if(Q.rear==Q.front)
return false; //队空报错
x=Q.data[Q.front]; //直接输出队头元素的值
return true;
}
如果不牺牲一个空间 怎么办呢??
方法二
用size记录队列当前的长度, 初始化时size=0;
插入成功 size++
删除成功size–
size=Maxsize 为队满
size=0 为队空
typedef struct{
ElemType data[Maxsize]; //静态数组存放队列元素
int front,rear; //队头指针和队尾指针
int size; //队列当前长度
}SqQueue;
方法三
在结构体中放入一个tag来进行记录最近是删除和插入,如
每次删除成功时,令tag=0;
每次插入成功时,令tag=1;
判断队满是:if(Q.frontQ.rear &&tag1)
判断队空是:if(Q.frontQ.rear &&tag====0)
typedef struct{
ElemType data[Maxsize]; //静态数组存放队列元素
int front,rear; //队头指针和队尾指针
int tag; //最近是删除或插入
}SqQueue;
6.判断队列中元素个数
队列元素个数=(rear+MaxSize-front)%MaxSize
还有一个:如果rear指针指向的是尾元素的时候,则入队的时候是
Q.rear=(Q.rear+1)%Maxsize;
Q.data[Q.rear]=x;
此时初始指针为
判队空:(Q.rear+1)%Maxsize==Q.front;
判队满:
牺牲个存储单元或增加辅助变量
边栏推荐
- 邻接矩阵无向图(一) - 基本概念与C语言
- Value/list in redis
- Wechat applet reports an error: [rendering layer network layer error] pages/main/main Local resource pictures in wxss cannot be obtained through wxss. You can use network pictures, Base64, or < image/
- [Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 2
- Mechanism and type of CPU context switch
- 【datawhale202206】pyTorch推荐系统:召回模型 DSSM&YoutubeDNN
- Onenet Internet of things platform - mqtts product equipment connected to the platform
- [Yunju entrepreneurial foundation notes] Chapter VII Entrepreneurial Resource test 1
- Prepare for the Blue Bridge Cup Day10__ PWM control light brightness
- Botu V15 add GSD file
猜你喜欢
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
Wechat applet reports an error: [rendering layer network layer error] pages/main/main Local resource pictures in wxss cannot be obtained through wxss. You can use network pictures, Base64, or < image/
The Missing Semester
基于IMDB评论数据集的情感分析
Compile and debug net6 source code
图的理论基础
深入理解 grpc part1
华为HMS Core携手超图为三维GIS注入新动能
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 6
Harbor webhook from principle to construction
随机推荐
耐克如何常年霸榜第一名?最新財報答案來了
Why does the JVM heap memory exceed 32g and pointer compression fail?
241. 为运算表达式设计优先级 : DFS 运用题
基于IMDB评论数据集的情感分析
Is it safe for Huatai Securities to open an account online?
伸展树(一) - 概念和C实现
Redis' attack tactics
二叉堆(一) - 原理与C实现
陈珙:微服务,它还那么纯粹吗?
Typora adds watermarks to automatically uploaded pictures
我在中山,到哪里开户比较好?实际上网上开户安全么?
指纹浏览器工作原理、使用场景以及重要性简单讲解
Neo4j 中文开发者月刊 - 202206期
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
[classic example] classic list questions @ list
91. (chapitre Cesium) simulation de lancement de fusées cesium
C summary of knowledge points 3
Uniapp uses uni upgrade Center
巩固-C#运算符
91. (cesium chapter) cesium rocket launch simulation