嵌入式C語言資料結構之迴圈佇列

2021-08-20 11:08:26 字數 2788 閱讀 5995

佇列擁有先進先出特性,類似生活中的排隊場景,多應用於緩衝區的設計等。

迴圈佇列就是當front或者rear即將產生陣列越界時,將其置為0。

本文用陣列實現迴圈佇列,感興趣的朋友也可以用鍊錶實現迴圈佇列。

以下為原始碼。

queue.h

#ifndef __queue_h__

#define __queue_h__

#include "stm32f10x.h"

#include #define maxqueuelength 20

#define loopqueue

typedef struct

queuenode;

typedef unsigned int queuestate;

queuestate queue_init(queuenode * phead);

queuestate queue_isempytorfull(queuenode * phead);

queuestate queue_addvalue(queuenode * phead,const uint32_t value);

queuestate queue_output(queuenode * phead, uint32_t *pbuffer , uint32_t nbytes);

#endif

queue.c

#include "queue.h"

/** * @name queue_init.

* @brief init my queue.

* @param listnode * phead: head of the queue.

* @retval 0 : succeed.

1 : head of queue is null

* @author jiao wo yi sheng xiao ming ge.

* @date 2018.5.30

**/queuestate queue_init(queuenode * phead)

/** * @name queue_isempytorfull.

* @brief get the queue state.

* @param listnode * phead: head of the queue.

* @retval 0 : not full or empty.

1 : the queue is empyt.

2 : the queue is full.

3 : head of queue is null

* @author jiao wo yi sheng xiao ming ge.

* @date 2018.5.30

**/queuestate queue_isempytorfull(queuenode * phead)

return 0;

}/**

* @name queue_addvalue.

* @brief add value to the queue.

* @param listnode * phead: head of the queue.

const uint32_t value: value added to the queue

* @retval 0 : succeed.

1 : the queue is full.

2 : head of queue is null.

* @author jiao wo yi sheng xiao ming ge.

* @date 2018.5.30

**/queuestate queue_addvalue(queuenode * phead, const uint32_t value)

static queuestate queue_outvalue(queuenode * phead, uint32_t *pbuffer)

/** * @name queue_output.

* @brief output values in queue to buffer.

* @param listnode * phead : head of the queue.

uint32_t * pbuffer: value added to the queue.if it's null,then clear the queue.

const uint32_t nbytes:output bytes numbers.

* @retval 0 : succeed.

1 : head of queue is null.

2 : length of output bytes is beyond queue.

* @author jiao wo yi sheng xiao ming ge.

* @date 2018.5.30

**/queuestate queue_output(queuenode * phead, uint32_t *pbuffer , uint32_t nbytes)

else

queue_init(phead);

return 0;

}

C語言資料結構之迴圈佇列

include define max 5 typedef struct queue queue initqueue queue q int isfull queue q else int isempty queue q else int insertelement queue q,int eleme...

資料結構 迴圈佇列 C語言

2 迴圈佇列源 及測試 佇列,顧名思義就像我們平時排隊打飯一樣,隊尾有人不斷來排隊打飯,隊頭不斷有人打完飯離開隊頭 順序佇列用順序儲存結構,即陣列儲存,分別包含倆個變數front和rear分別代表隊頭和隊尾,為了防止陣列越界溢位,我們將順序佇列變成乙個環狀的空間,即迴圈佇列,超出陣列界隊尾重新回到陣...

資料結構之迴圈佇列(C語言實現)

具體程式實現如下 快取佇列長度 define cirqueue len 200 typedef unsigned char u8 typedef struct cirqueue 迴圈佇列 static cirqueue cirqueue 迴圈佇列初始化 void cqinit void 檢查佇列是否...