????????隊列是一種常見的數(shù)據(jù)結(jié)構(gòu),它具有先進(jìn)先出(First-In-First-Out,F(xiàn)IFO)的特性,類似于排隊等候的場景。以下是隊列的要點:
1. 定義:隊列是一種線性數(shù)據(jù)結(jié)構(gòu),由一系列元素組成,可以進(jìn)行插入和刪除操作。插入操作(稱為入隊)只能在隊列的末尾進(jìn)行,刪除操作(稱為出隊)只能從隊列的前端進(jìn)行。
2. 特性:隊列遵循先進(jìn)先出的原則,最先入隊的元素將最先出隊。
3. 基本操作:
? ?- 入隊(Enqueue):將元素插入到隊列的末尾。
? ?- 出隊(Dequeue):從隊列的前端刪除一個元素,并返回刪除的元素。
? ?- 隊列是否為空(isEmpty):判斷隊列是否為空,即沒有任何元素。
? ?- 隊列長度(size):返回隊列中元素的個數(shù)。
4. 實現(xiàn)方式:
? ?- 數(shù)組:使用數(shù)組實現(xiàn)隊列時,需要維護(hù)兩個指針,一個指向隊列的前端,另一個指向隊列的末尾。出隊時移動前端指針,入隊時移動末尾指針。注意需要循環(huán)利用數(shù)組空間。
? ?- 鏈表:使用鏈表實現(xiàn)隊列時,新元素可以直接添加到鏈表末尾,出隊時刪除鏈表的頭節(jié)點。
5. 隊列的應(yīng)用:
? ?- 廣度優(yōu)先搜索算法(BFS):在圖的遍歷中,廣度優(yōu)先搜索需要使用隊列來實現(xiàn)層次遍歷。
? ?- 計算機(jī)任務(wù)調(diào)度:操作系統(tǒng)中的任務(wù)調(diào)度可以使用隊列來管理任務(wù)的執(zhí)行順序。
? ?- 隊列作為其他數(shù)據(jù)結(jié)構(gòu)的輔助結(jié)構(gòu):例如,樹的層次遍歷、圖的廣度優(yōu)先搜索等。
6. 常見類型:
? ?- 普通隊列(普通隊列):遵循FIFO原則,用于常規(guī)的數(shù)據(jù)排隊。
? ?- 優(yōu)先隊列(Priority Queue):在出隊時按照優(yōu)先級進(jìn)行排序,元素的出隊順序不一定按照插入順序。
????????隊列在計算機(jī)科學(xué)中具有廣泛的應(yīng)用,從操作系統(tǒng)到算法設(shè)計都有著重要作用。它是解決許多問題的重要工具之一。
順序表隊列文章來源:http://www.zghlxwxcb.cn/news/detail-666442.html
/*===============================================
* 文件名稱:queue.c
* 創(chuàng) 建 者:WM
* 創(chuàng)建日期:2023年08月21日
* 描 述:順序隊列//下標(biāo)為rear里沒有數(shù)據(jù)
================================================*/
#include <stdio.h>
#include<stdlib.h>
#define SIZE 8
typedef int data_t;
//構(gòu)造節(jié)點類型
typedef struct node{
data_t data[SIZE];//保存數(shù)據(jù)的數(shù)據(jù)域
data_t front;
data_t rear;
} sequeue;
sequeue *createEmptySequeue()
{
sequeue *p = (sequeue *)malloc(sizeof(sequeue));
if(NULL == p)
{
perror("createEmptySequeue malloc failed");
return NULL;
}
//只要你申請空間就是為了讓他裝上數(shù)據(jù)
p->rear = 0;//使用的時候是數(shù)組的下標(biāo)
p->front = 0;//使用的時候是數(shù)組的下標(biāo)
return p;
}
int insert(sequeue* sq,data_t h)
{
sq->data[sq->rear]=h;
sq->rear=(sq->rear+1)%SIZE;//注意
}
int out_queue(sequeue *sq)
{
data_t val=sq->data[sq->front];
sq->front=(sq->front+1)%SIZE;
printf("%d \n",val);
return val;
}
int isQueue_empty(sequeue *sq)
{
if(sq==NULL) -1;
return sq->front==sq->rear;
}
//注意
int isQueue_full(sequeue *sq)
{
//return (sq->rear-sq->front+SIZE)%SIZE==SIZE-1;//這個算法很重要
return (sq->rear+1) % SIZE == sq->front;//或者這個。
}
//注意
int isQueue_full2(sequeue*sq)
{
if(sq->front>sq->rear)
return sq->front-sq->rear==1;
if(sq->front<sq->rear)
return sq->rear-sq->front==SIZE-1;
}
int queue_num(sequeue* sq)//誰大誰在前面。
{
return (sq->front<=sq->rear)?(sq->rear-sq->front):(sq->rear-sq->front+SIZE);
}
void clear_queue(sequeue *sq)
{
while (!isQueue_empty(sq))
out_queue(sq);
}
int main(int argc, char *argv[])
{
sequeue*phead=createEmptySequeue();
for (int i = 0; i < SIZE-1; i++)
{
insert(phead,i+1);
}
out_queue(phead);
printf("%d \n",queue_num(phead));
return 0;
}
鏈表隊列文章來源地址http://www.zghlxwxcb.cn/news/detail-666442.html
/*===============================================
* 文件名稱:queue.c
* 創(chuàng) 建 者:WM
* 創(chuàng)建日期:2023年08月21日
* 描 述:鏈表隊列
================================================*/
#include <stdio.h>
#include<stdlib.h>
typedef int data_t;
//構(gòu)造鏈表節(jié)點類型
typedef struct node{
data_t data;//保存數(shù)據(jù)的數(shù)據(jù)域
struct node*next;//保存下一個節(jié)點的地址
} linklist ;
typedef struct {
linklist *front;
linklist* rear;
} lqueue;
lqueue* creat_lqueue()
{
lqueue*lq=(lqueue*)malloc(sizeof(lqueue));
lq->front=(linklist*)malloc(sizeof(linklist));
lq->front->next=NULL;
lq->rear=lq->front;
return lq;
}
int insert(lqueue* lq,data_t h)
{
linklist *new=(linklist *)malloc(sizeof(linklist));
if(NULL==new) return -1;
new->data=h;
new->next=NULL;
lq->rear->next=new;
lq->rear=new;
}
int out_queue(lqueue*lq)
{
linklist* m=lq->front->next;
lq->front->next=m->next;
int val=m->data;
free(m);
m=NULL;
printf("%d \n",val);
return val;
}
int isQueue_empty(lqueue*lq)
{
return lq->front==lq->rear;
}
int queue_num(lqueue*lq)
{
int len=0;
linklist* h = lq->front;
while (h->next!=NULL)
{
h=h->next;
len++;
}
return len;
}
void clear_queue(lqueue*lq)
{
while (!isQueue_empty(lq))
out_queue(lq);
}
int main(int argc, char *argv[])
{
lqueue*lqhead=creat_lqueue();
insert(lqhead,9);
insert(lqhead,110);
printf("%d \n",queue_num(lqhead));
out_queue(lqhead);
out_queue(lqhead);
printf("%d \n",queue_num(lqhead));
clear_queue(lqhead);
printf("%d \n",queue_num(lqhead));
return 0;
}
到了這里,關(guān)于數(shù)據(jù)結(jié)構(gòu),隊列,順序表隊列,鏈表隊列的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!