隊(duì)列的特點(diǎn):
先進(jìn)先出,隊(duì)列是一種操作受限的線性表,其限制條件為允許在表的一端進(jìn)行插入,而在表的另一端進(jìn)行刪除。插入的一端叫做隊(duì)尾,刪除的一端叫做隊(duì)頭。向隊(duì)列中插入新元素的行為稱為進(jìn)隊(duì),從隊(duì)列中刪除元素的行為稱為出隊(duì)。一般用法在隊(duì)頭插入,在隊(duì)尾刪除。
隊(duì)列終究它就是個(gè)buf,buf就是存東西的,保存不丟失,可以理解為在buf上封裝了一些操作。
隊(duì)列的用處:
1、用于任務(wù)間通信,尤其對(duì)于單任務(wù)系統(tǒng),多個(gè)任務(wù)都操作一個(gè)隊(duì)列,比如,A任務(wù)往隊(duì)列里扔數(shù)據(jù),B任務(wù)負(fù)責(zé)檢查隊(duì)列,只要有數(shù)據(jù)就進(jìn)行處理,起到了兩個(gè)任務(wù)通信的效果,同樣可以把這個(gè)數(shù)據(jù)理解為消息,A任務(wù)給B任務(wù)發(fā)消息,B任務(wù)根據(jù)不同的消息(數(shù)據(jù))做不同的處理。
2、緩沖數(shù)據(jù)。最常見的就是串口接收數(shù)據(jù),搞一個(gè)環(huán)形buf隊(duì)列,收到數(shù)據(jù)就放到這個(gè)隊(duì)列中,當(dāng)然這個(gè)buf肯定是有長(zhǎng)度的,不能一直存儲(chǔ)數(shù)據(jù)。正常用法下,得及時(shí)讀取走,這樣就能一直接收數(shù)據(jù)不丟失。
隊(duì)列的幾個(gè)基本的使用包括:
- 隊(duì)列的創(chuàng)建;
- 隊(duì)列的添加元素,刪除元素,提取元素等對(duì)隊(duì)列的操作;
隊(duì)列的創(chuàng)建:首先定義一個(gè)隊(duì)列的大小,這里直接定義為int型數(shù)組,最大存儲(chǔ)6個(gè)元素。定義一個(gè)結(jié)構(gòu)體存放隊(duì)列的信息,包括數(shù)組的指針,用于后續(xù)對(duì)數(shù)組的操作,定義好結(jié)構(gòu)體后對(duì)結(jié)構(gòu)體進(jìn)行初始化
int queuebuffer[6]={0};typedef struct _Tag_My_Queue_{ int *buffer; unsigned char In; unsigned char Out; int queueSize;}my_queue;void queueInit(my_queue *queue,int *buffer,int bufferSize){ queue->buffer = buffer; queue->In = 0; queue->Out = 0; queue->queueSize = bufferSize;}
隊(duì)列空間 與隊(duì)列數(shù)據(jù)數(shù)量的函數(shù)如下:
int queue_space(my_queue *queue){ int size =queue->queueSize; int num = queue->Out; while(num != queue->In) { size--; num = (num + 1)%queue->queueSize; } return size - 1;}//queue data numbersint queue_deep(my_queue *queue){ int size = 0; int num = queue->Out; while(num != queue->In) { size++; num = (num + 1)%queue->queueSize; } return size;}
如果要對(duì)隊(duì)列增加和刪除元素要進(jìn)行隊(duì)列的相關(guān)操作。定義函數(shù)如下:
int addDataToQueue(my_queue *queue,int data){ if(queue_full(queue)) { printf("queue already full!!!\n"); return 0; } queue->buffer[queue->In] = data; queue->In = (queue->In + 1) % queue->queueSize; return 1;}int deleteDataFromQueue(my_queue *queue,int *data){ if(queue_empty(queue)) { printf("queue already empty!!!\n"); return 0; } *data = queue->buffer[queue->Out]; queue->Out = (queue->Out + 1) % queue->queueSize; return 1;}
隊(duì)列生成后要進(jìn)行隊(duì)列的元素判斷,隊(duì)列是否為空,或者是否隊(duì)列已滿,定義函數(shù)如下:
int addDataToQueue(my_queue *queue,int data){ if(queue_full(queue)) { printf("queue already full!!!\n"); return 0; } queue->buffer[queue->In] = data; queue->In = (queue->In + 1) % queue->queueSize; return 1;}int deleteDataFromQueue(my_queue *queue,int *data){ if(queue_empty(queue)) { printf("queue already empty!!!\n"); return 0; } *data = queue->buffer[queue->Out]; queue->Out = (queue->Out + 1) % queue->queueSize; return 1;}
下面就對(duì)隊(duì)列 的數(shù)據(jù)進(jìn)行相關(guān)的操作,包括增加數(shù)據(jù)入隊(duì)列,數(shù)據(jù)出隊(duì)列等相關(guān)的操做,運(yùn)行函數(shù)觀察運(yùn)行起來的狀態(tài)。
void main(){ my_queue testqueue; int data; queueInit(&testqueue,queuebuffer,sizeof(queuebuffer)/sizeof(queuebuffer[0])); addDataToQueue(&testqueue,55); addDataToQueue(&testqueue,3); addDataToQueue(&testqueue,2); addDataToQueue(&testqueue,33); addDataToQueue(&testqueue,6);printf("queue data is %d\r\n",queue_print(&testqueue)); printf("---queue deep:%d\n",queue_deep(&testqueue)); printf("---queue space:%d\n",queue_space(&testqueue)); deleteDataFromQueue(&testqueue,&data); printf("data:%d\n",data); deleteDataFromQueue(&testqueue,&data); printf("data:%d\n",data); deleteDataFromQueue(&testqueue,&data); printf("data:%d\n",data); deleteDataFromQueue(&testqueue,&data); printf("data:%d\n",data); printf("---queue deep:%d\n",queue_deep(&testqueue)); printf("---queue space:%d\n",queue_space(&testqueue)); deleteDataFromQueue(&testqueue,&data); printf("data:%d\n",data); addDataToQueue(&testqueue,45); addDataToQueue(&testqueue,22); addDataToQueue(&testqueue,33); deleteDataFromQueue(&testqueue,&data); printf("data:%d\n",data); printf("data:%d\n",deleteDataFromQueue(&testqueue,&data)); printf("---queue deep:%d\n",queue_deep(&testqueue)); printf("---queue space:%d\n",queue_space(&testqueue)); queue_print(&testqueue); printf("In:%d,Out:%d\n",testqueue.In,testqueue.Out);while(1);}
運(yùn)行結(jié)果如下圖所示:
運(yùn)行結(jié)果
-
數(shù)據(jù)
+關(guān)注
關(guān)注
8文章
7002瀏覽量
88941 -
函數(shù)
+關(guān)注
關(guān)注
3文章
4327瀏覽量
62569 -
隊(duì)列
+關(guān)注
關(guān)注
1文章
46瀏覽量
10893
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論