int queue_is_full(Queue *queue) { return ((queue->tail + 1) % queue->size) == queue->head; } int queue_is_empty(Queue *queue) { return queue->tail == queue->head; } int queue_enqueue(Queue *queue, uint8_t item) { if (!queue_is_full(queue)) { queue->data[queue->tail++] = item; queue->tail %= queue->size; return 1; } else { uart_disable(); return 0; } } int queue_dequeue(Queue *queue, uint8_t *item) { if (!queue_is_empty(queue)) { *item = queue->data[queue->head++]; queue->head %= queue->size; return 1; } else { return 0; } }
时间: 2024-04-26 19:20:53 浏览: 10
这是一个基于循环数组实现的队列。其中,queue_is_full函数用于判断队列是否已满,如果队列的尾指针加1对队列的长度取余等于头指针,即队列满了;queue_is_empty函数用于判断队列是否为空,如果队列的头指针等于尾指针,即队列为空。queue_enqueue函数用于向队列中加入元素,它首先判断队列是否已满,如果未满,则将元素添加到队列的尾部,并将尾指针向后移动一个位置,最后对尾指针取模以实现循环队列的效果。如果队列已满,则禁用串口并返回0。queue_dequeue函数用于从队列中取出元素,它首先判断队列是否为空,如果非空,则将队列头部的元素赋值给指针item,并将头指针向后移动一个位置,最后对头指针取模以实现循环队列的效果。如果队列为空,则返回0。
相关问题
把这个结构体更改命名为charbuff,并修改下列函数变量typedef struct { uint8_t *data; //!< Array of data, stored on the heap. uint32_t head; //!< Index in the array of the oldest element. uint32_t tail; //!< Index in the array of the youngest element. uint32_t size; //!< Size of the data array. } Queue; Queue charBuf; int queue_init(Queue *queue) { const uint32_t size = 4; queue->data = (uint8_t*)malloc(sizeof(uint8_t) * size); queue->head = 0; queue->tail = 0; queue->size = size; // If malloc returns NULL (0) the allocation has failed. return queue->data != 0; } int queue_is_full(Queue *queue) { return ((queue->tail + 1) == queue->head; } int queue_is_empty(Queue *queue) { return queue->tail == queue->head; } int queue_enqueue(Queue *queue, uint8_t item) { if (!queue_is_full(queue)) { queue->data[queue->tail++] = item; queue->tail %= queue->size; return 1; } else { uart_disable(); return 0; } } int queue_dequeue(Queue *queue, uint8_t *item) { if (!queue_is_empty(queue)) { *item = queue->data[queue->head++]; queue->head %= queue->size; return 1; } else { return 0; } }
如果将结构体重命名为`charbuff`,可以将代码修改为如下形式:
```
typedef struct {
uint8_t *data; //!< Array of data, stored on the heap.
uint32_t head; //!< Index in the array of the oldest element.
uint32_t tail; //!< Index in the array of the youngest element.
uint32_t size; //!< Size of the data array.
} charbuff;
charbuff charBuf;
int queue_init(charbuff *queue) {
const uint32_t size = 4;
queue->data = (uint8_t*)malloc(sizeof(uint8_t) * size);
queue->head = 0;
queue->tail = 0;
queue->size = size;
// If malloc returns NULL (0) the allocation has failed.
return queue->data != 0;
}
int queue_is_full(charbuff *queue) {
return ((queue->tail + 1) == queue->head);
}
int queue_is_empty(charbuff *queue) {
return queue->tail == queue->head;
}
int queue_enqueue(charbuff *queue, uint8_t item) {
if (!queue_is_full(queue)) {
queue->data[queue->tail++] = item;
queue->tail %= queue->size;
return 1;
} else {
uart_disable();
return 0;
}
}
int queue_dequeue(charbuff *queue, uint8_t *item) {
if (!queue_is_empty(queue)) {
*item = queue->data[queue->head++];
queue->head %= queue->size;
return 1;
} else {
return 0;
}
}
```
这样就将结构体重命名为`charbuff`,并将函数参数中的`Queue`类型改为`charbuff`类型。这样做可以使代码更加清晰易懂,减少歧义和误解。同时,也需要将变量名统一修改为`charBuf`。
设置这个队列长度为8的时候判为满int queue_is_full(Queue *queue) { return ((queue->tail + 1) % queue->size) == queue->head; } int queue_is_empty(Queue *queue) { return queue->tail == queue->head; } int queue_enqueue(Queue *queue, uint8_t item) { if (!queue_is_full(queue)) { queue->data[queue->tail++] = item; queue->tail %= queue->size; return 1; } else { uart_disable(); return 0; } } int queue_dequeue(Queue *queue, uint8_t *item) { if (!queue_is_empty(queue)) { *item = queue->data[queue->head++]; queue->head %= queue->size; return 1; } else { return 0; } }
如果设置这个队列长度为8,那么队列满的判断应该是:
```
int queue_is_full(Queue *queue) {
return ((queue->tail + 1) % queue->size) == (queue->head % queue->size);
}
```
这里需要将头指针和尾指针对队列长度取模,以防止指针越界。