日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

C語言實(shí)現(xiàn)順序循環(huán)隊(duì)列實(shí)例_C 語言

作者:犀牛超人 ? 更新時(shí)間: 2022-04-16 編程語言

一、隊(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

欄目分類
最近更新