生成一段由链表结构体形成环形缓冲区的代码
时间: 2023-03-15 14:21:32 浏览: 66
示例环形缓冲区的代码:#include<stdio.h>
#define MAX_SIZE 10 struct node
{
int data;
struct node *next;
}; struct node *head = NULL;
struct node *last = NULL; // 初始化环形缓冲区
void initCircularBuffer(){
head = last = NULL;
} // 添加结点到环形缓冲区
void addNode(int data){
struct node *temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
temp->next = NULL;
if (head == NULL)
{
head = last = temp;
last->next = head;
}
else
{
last->next = temp;
last = temp;
last->next = head;
}
} // 删除结点从环形缓冲区
void deleteNode(){
struct node *temp;
if (head == NULL)
{
printf("Buffer is empty \n");
}
else if (head == last)
{
temp = head;
head = last = NULL;
free(temp);
}
else
{
temp = head;
head = head->next;
last->next = head;
free(temp);
}
} int main()
{
initCircularBuffer();
addNode(1);
addNode(2);
deleteNode();
return 0;
}