C語言實現(xiàn)循環(huán)隊列基本操作
更新時間:2021年09月24日 08:30:43 作者:似曾不相識
這篇文章主要為大家詳細介紹了C語言實現(xiàn)循環(huán)隊列基本操作,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
循環(huán)隊列依靠取模運算,實現(xiàn)隊列中數(shù)據(jù)元素的邏輯成環(huán)操作。其相比隊列的順序存儲實現(xiàn),可以避免“假溢出”的問題。
頭文件聲明
#include <stdio.h>
#include <stdlib.h>
/*
* 循環(huán)隊列實現(xiàn)
*/
//數(shù)據(jù)元素上限
#define MaxSize 50
//定義數(shù)據(jù)類型
typedef int ElemType;
/*結(jié)構(gòu)體定義*/
typedef struct SqQueue
{
ElemType data[MaxSize];//數(shù)組-存放數(shù)據(jù)元素
int front, //隊頭指針
rear; //隊尾指針
}SqQueue;
//初始化隊列
void InitQueue(SqQueue *q);
//判斷隊列是否為空
int EmptyQueue(SqQueue q);
//入隊操作
int EnQueue(SqQueue *q,ElemType e);
//出隊操作
int DeQueue(SqQueue *q,ElemType* e);
//獲取隊列長度
int LengthQueue(SqQueue q);
//獲取隊頭元素
void GetHead(SqQueue q,ElemType* e);
//打印隊列
void printSqQueue(SqQueue q);
函數(shù)實現(xiàn)
#include "SqQueue.h"
/**
* 循環(huán)隊列函數(shù)實現(xiàn)
*/
//初始化隊列
void InitQueue(SqQueue *q){
//隊頭指針-隊尾指針,同時指向隊首元素
q->front=q->rear=0;
}
//判斷隊列是否為空
int EmptyQueue(SqQueue q){
//隊頭指針和隊尾指針指向同一個元素,則為空隊列
return q.front==q.rear;
}
//入隊操作
int EnQueue(SqQueue *q,ElemType e){
//判斷是否隊滿
if ((q->rear+1)%MaxSize==q->rear)
return -1;
//入隊操作
q->data[q->rear]=e;//添加數(shù)據(jù)元素-將隊尾元素值置為e
q->rear=(q->rear+1)%MaxSize;//尾指針向前移動,隊尾指針++
return 1;
}
//出隊操作
int DeQueue(SqQueue *q,ElemType* e){
//判斷是否隊空
if ((q->rear+1)%MaxSize==q->front)
return -1;
//保存數(shù)據(jù)
*e=q->data[q->front];
//出隊操作
q->front=(q->front+1)%MaxSize;
return 1;
}
//獲取隊列長度
int LengthQueue(SqQueue q){
return (q.rear-q.front+MaxSize)%MaxSize;
}
//獲取隊頭元素
void GetHead(SqQueue q,ElemType* e){
//判斷隊列是否為空
if (q.front==q.rear)
return;
//獲取隊頭元素的值
*e=q.data[q.front];
}
//打印隊列
void printSqQueue(SqQueue q){
//輔助指針
int pIndex;
//打印隊列元素
pIndex=q.front;
while (pIndex<q.rear)
{
printf("%4d",q.data[pIndex++]);
}
printf("\n");
}
函數(shù)測試
#include "SqQueue.h"
int main(int argc,char** argv){
//聲明隊列
SqQueue sQueue;
int i;
ElemType data;
//初始化隊列
InitQueue(&sQueue);
//獲取隊頭指針和隊尾指針的值
printf("frontVal=%d,rearVal=%d\n",sQueue.front,sQueue.rear);
//判斷隊列是否為空
printf("is Empty?%d\n",EmptyQueue(sQueue));
//入隊操作
for (i=0;i<20;i++)
{
EnQueue(&sQueue,i+1);
}
//判斷隊列是否為空&獲取隊列長度
printf("is Empty?%d,len=%d\n",EmptyQueue(sQueue),LengthQueue(sQueue));
//打印隊列元素
printSqQueue(sQueue);
//出隊操作
DeQueue(&sQueue,&data);
printf("the aimed value is %d\n",data);
//判斷隊列是否為空&獲取隊列長度
printf("is Empty?%d,len=%d\n",EmptyQueue(sQueue),LengthQueue(sQueue));
//打印隊列元素
printSqQueue(sQueue);
//獲取隊頭元素值
GetHead(sQueue,&data);
printf("the head value is %d\n",data);
return 0;
}
再貼個測試結(jié)果的圖:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++中Overload,Override,Hide之間的區(qū)別
重載overload,這個概念是大家熟知的。在同一可訪問區(qū)內(nèi)被聲名的幾個具有不同參數(shù)列的(參數(shù)的類型、個數(shù)、順序不同)同名函數(shù),程序會根據(jù)不同的參數(shù)列來確定具體調(diào)用哪個函數(shù),這種機制就是重載2013-09-09
C語言入門學習之fgets()函數(shù)和fputs()函數(shù)
fgetc() 和 fputc() 函數(shù)每次只能讀寫一個字符,速度較慢,實際開發(fā)中往往是每次讀寫一個字符串或者一個數(shù)據(jù)塊,這樣能明顯提高效率,這篇文章主要給大家介紹了關(guān)于C語言入門學習之fgets()函數(shù)和fputs()函數(shù)的相關(guān)資料,需要的朋友可以參考下2021-11-11
C++設(shè)計模式編程之Flyweight享元模式結(jié)構(gòu)詳解
這篇文章主要介紹了C++設(shè)計模式編程的Flyweight享元模式結(jié)構(gòu),享元模式在實現(xiàn)過程中主要是要為共享對象提供一個存放的"倉庫"(對象池),需要的朋友可以參考下2016-03-03

