網(wǎng)站首頁 編程語言 正文
一、隊(duì)列和循環(huán)隊(duì)列基本概念
隊(duì)列:
和棧相反,隊(duì)列是一種先進(jìn)先出(FIFO)的線性表。只允許在一端插入,在另一端刪除。
允許插入的叫"隊(duì)尾"(rear),允許刪除的叫"隊(duì)頭"(front)。
入隊(duì)操作:L->rear++;?? L->data[L->rear]=x;(需要入隊(duì)的元素)
出隊(duì)操作:L->front++;? x=L->data[L->front];
求隊(duì)長:隊(duì)長=(L->rear)-(L->front);
隊(duì)空:L->rear==L->front==-1;
隊(duì)滿:隊(duì)長=max
使用場景:一切具備先來后到的任務(wù)場景
循環(huán)隊(duì)列:
??和隊(duì)列類似,只不過隊(duì)頭和隊(duì)尾相連,解決了隊(duì)列的假溢出現(xiàn)象(隊(duì)尾指針達(dá)到申請空間的最大時(shí),系統(tǒng)會認(rèn)定空間滿,但是隊(duì)頭指針如果不為-1則就是假溢出)。
入隊(duì):L->rear==(L->rear+1)%max;
出隊(duì):L->front==(L->front+1)%max;
隊(duì)滿:由圖可知 隊(duì)滿和隊(duì)空條件重復(fù),所以為了避免這一情況現(xiàn)如今有兩種方法:1.少用一個(gè)元素空間,也就是說front指針在定義時(shí)指向0的位置,這樣才能使front和rear之間空出一個(gè)元素空間。2.這個(gè)方法比較容易理解,就是定義一個(gè)Num值來記錄元素個(gè)數(shù),num==0時(shí)則隊(duì)空,num==max時(shí)則隊(duì)滿。
具體操作:(L->rear+1)%max==front; ?num==max;
隊(duì)空:L->rear==L->front;
隊(duì)長:分兩種情況:1.頭指針在尾指針之后:按普通隊(duì)列求法。2.頭指針在尾指針之前:隊(duì)長=L->rear+(max-(L->front));
二、代碼實(shí)操
?圖示:
?具體代碼:
#include<stdio.h> #include<stdlib.h> #include<string.h> #define ture 1 #define false 0 #define max 5 typedef struct { int data[max]; int front,rear; }cteam,*team; static int num=0; //全局變量 num //初始隊(duì)列 int Initteam(team &L){ L=(cteam *)malloc(sizeof(cteam)); if(L==NULL){ printf("申請空間失?。?); return false; } L->front=L->rear=-1; return true; } //入隊(duì) int pushteam(team &L,int x){ if(num==max){ printf("隊(duì)滿"); return false; }else{ L->rear=(L->rear+1)%max; //rear始終在0-10中循環(huán) L->data[L->rear]=x; num++; return true; } } //出隊(duì) int popteam(team &L,int &x){ if(num==0){ printf("隊(duì)空!"); return false; }else{ L->front=(L->front+1)%max; //front始終在0-10中循環(huán) x=L->data[L->front]; num--; printf("\n%d出隊(duì)\n",x); return x; } } //遍歷隊(duì) void printteam(team L){ int p; p=L->front+1; if(L->front<L->rear){ while(p<=L->rear){ printf("%d ",L->data[p]); p++;} }else{ while((p-1)!=L->rear){ printf("%d ",L->data[p]); p=(p+1)%max; } } } //求隊(duì)長 int teamlength(team L){ int p; if(L->front<L->rear){ p=(L->rear)-(L->front); //當(dāng)隊(duì)列正常時(shí) }else{ p=L->rear+(max-(L->front)); //當(dāng)隊(duì)列開始循環(huán)時(shí) } printf("\n隊(duì)長為:%d",p); } //取隊(duì)頭元素 int gettop(team L){ if(L->front==L->rear){ printf("隊(duì)空!"); return false; }else{ int t=L->data[L->front+1]; return t; } } /* pushteam:入隊(duì)函數(shù) popteam:出隊(duì)函數(shù) printteam:遍歷函數(shù) gettop:取隊(duì)頭函數(shù) Initteam:初始化函數(shù) teamlength:求隊(duì)長函數(shù) */ int main(){ team s; int w; Initteam(s); //1-3進(jìn)隊(duì)列 pushteam(s,1); pushteam(s,2); pushteam(s,3); printf("------1-3進(jìn)隊(duì)列后----------\n"); printf("此時(shí)隊(duì)列為:"); printteam(s); popteam(s,w); popteam(s,w); printf("此時(shí)隊(duì)列為:"); printteam(s); printf("\n------4-6進(jìn)隊(duì)列后----------\n"); pushteam(s,4); pushteam(s,5); pushteam(s,6); printf("此時(shí)隊(duì)列為:"); printteam(s); teamlength(s); int T=gettop(s); printf("\n隊(duì)頭元素為:%d",T); }
實(shí)現(xiàn)結(jié)果:
總結(jié)
原文鏈接:https://blog.csdn.net/m0_61395860/article/details/122847003
相關(guān)推薦
- 2022-05-25 RedisTemplate實(shí)現(xiàn)setnx分布式鎖
- 2022-05-19 詳解QTreeWidget隱藏節(jié)點(diǎn)的兩種方式_C 語言
- 2022-08-28 linux--network和NetManager沖突導(dǎo)致network[44649]:RTNETL
- 2022-01-17 ubuntu出現(xiàn)RPM should not be used directly install RP
- 2023-04-02 淺談Android開發(fā)Webview的Loading使用效果_Android
- 2022-09-04 ffmpeg網(wǎng)頁視頻流m3u8?ts實(shí)現(xiàn)視頻下載_相關(guān)技巧
- 2022-06-26 oracle中dblink查看、創(chuàng)建、使用以及刪除實(shí)例代碼_oracle
- 2022-06-18 Elasticsearches之python使用及Django與Flask集成示例_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支