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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Windows C++ 應(yīng)用程序通用日志組件的使用詳解
眾所周知,在調(diào)試、跟蹤和執(zhí)行應(yīng)用程序的過程中,程序的日志能為這些工作提供大量有價值的運行信息。因此,程序的日志對應(yīng)用程序的運行、維護至關(guān)重要2013-05-05淺談返回函數(shù)內(nèi)部new分配的內(nèi)存的引用
下面小編就為大家?guī)硪黄獪\談返回函數(shù)內(nèi)部new分配的內(nèi)存的引用。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12C++?opencv圖像處理使用cvtColor實現(xiàn)顏色轉(zhuǎn)換
這篇文章主要為大家介紹了C++?opencv圖像處理cvtColor實現(xiàn)顏色轉(zhuǎn)換的實現(xiàn)示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05