C語言實(shí)現(xiàn)順序循環(huán)隊(duì)列實(shí)例
一、隊(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)會(huì)認(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é)
到此這篇關(guān)于C語言實(shí)現(xiàn)順序循環(huán)隊(duì)列實(shí)例的文章就介紹到這了,更多相關(guān)C語言順序循環(huán)隊(duì)列內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VS Code 中搭建 Qt 開發(fā)環(huán)境方案分享
這篇文章主要介紹了VS Code 中搭建 Qt 開發(fā)環(huán)境方案分享的相關(guān)資料,需要的朋友可以參考下2022-12-12
Cocos2d-x學(xué)習(xí)筆記之Hello World!
這篇文章主要介紹了Cocos2d-x學(xué)習(xí)筆記之Hello World!本文基于vs2010和C++語言開發(fā),需要的朋友可以參考下2014-09-09
C++詳解默認(rèn)參數(shù)的構(gòu)造函數(shù)及簡單實(shí)例代碼
這篇文章主要介紹了 C++詳解默認(rèn)參數(shù)的構(gòu)造函數(shù)及簡單實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02
C/C++?Linux?Socket網(wǎng)絡(luò)編程流程分析
這篇文章主要介紹了C/C++?Linux?Socket網(wǎng)絡(luò)編程,Linux環(huán)境中的C/C++?socket?與Window環(huán)境中的C/C++?socket類似,本文所記錄的是TCP協(xié)議的socket編程,圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02

