用结构体编写FIFO函数
时间: 2023-11-30 16:02:04 浏览: 52
下面是一个用结构体编写FIFO(First In First Out)函数的示例代码:
```c
#include <stdio.h>
#include <stdbool.h>
#define MAX_SIZE 10
typedef struct {
int buffer[MAX_SIZE]; // 存储数据的缓冲区
int head; // 队列头指针
int tail; // 队列尾指针
bool is_empty; // 标识队列是否为空
bool is_full; // 标识队列是否已满
} fifo;
// 初始化FIFO队列
void fifo_init(fifo *f) {
f->head = 0;
f->tail = 0;
f->is_empty = true;
f->is_full = false;
}
// 向FIFO队列中插入一个元素
void fifo_push(fifo *f, int data) {
if (f->is_full) {
printf("FIFO is full, can't push data.\n");
return;
}
f->buffer[f->tail] = data;
f->tail++;
if (f->tail >= MAX_SIZE) {
f->tail = 0;
}
if (f->tail == f->head) {
f->is_full = true;
}
f->is_empty = false;
}
// 从FIFO队列中取出一个元素
int fifo_pop(fifo *f) {
if (f->is_empty) {
printf("FIFO is empty, can't pop data.\n");
return -1;
}
int data = f->buffer[f->head];
f->head++;
if (f->head >= MAX_SIZE) {
f->head = 0;
}
if (f->head == f->tail) {
f->is_empty = true;
}
f->is_full = false;
return data;
}
// 获取FIFO队列中元素的数量
int fifo_count(fifo *f) {
if (f->is_empty) {
return 0;
}
if (f->is_full) {
return MAX_SIZE;
}
if (f->head < f->tail) {
return f->tail - f->head;
} else {
return MAX_SIZE - f->head + f->tail;
}
}
// 打印FIFO队列中的元素
void fifo_print(fifo *f) {
if (f->is_empty) {
printf("FIFO is empty.\n");
return;
}
printf("FIFO elements: ");
int count = fifo_count(f);
int i = f->head;
while (count > 0) {
printf("%d ", f->buffer[i]);
i++;
if (i >= MAX_SIZE) {
i = 0;
}
count--;
}
printf("\n");
}
int main() {
fifo f;
fifo_init(&f);
fifo_push(&f, 1);
fifo_push(&f, 2);
fifo_push(&f, 3);
fifo_push(&f, 4);
fifo_push(&f, 5);
fifo_print(&f);
printf("fifo_pop: %d\n", fifo_pop(&f));
printf("fifo_pop: %d\n", fifo_pop(&f));
fifo_push(&f, 6);
fifo_push(&f, 7);
fifo_print(&f);
return 0;
}
```
在上面的代码中,我们定义了一个名为`fifo`的结构体,用于存储FIFO队列的相关信息,包括一个存储数据的缓冲区`buffer`,一个队列头指针`head`,一个队列尾指针`tail`,以及标识队列是否为空和已满的变量`is_empty`和`is_full`。然后,我们实现了四个FIFO队列操作函数:`fifo_init`用于初始化队列,`fifo_push`用于向队列中插入一个元素,`fifo_pop`用于从队列中取出一个元素,`fifo_count`用于获取队列中元素的数量。最后,我们在`main`函数中使用这些函数来操作FIFO队列,并打印队列中的元素。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)