InsCode AI创作助手
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 10
int buffer[BUFFER_SIZE]; // 环形缓冲区数组
int readIndex = 0; // 缓冲区读取索引
int writeIndex = 0; // 缓冲区写入索引
int count = 0; // 缓冲区占用计数器
void enqueue(int data) {
if (count == BUFFER_SIZE) { // 缓冲区已满
printf("Buffer is full. Unable to enqueue %d.\n", data);
return;
}
buffer[writeIndex] = data;
writeIndex = (writeIndex + 1) % BUFFER_SIZE; // 环形
count++;
if (count == BUFFER_SIZE / 2) { // 缓冲区占用了一半
printf("Buffer is 50%% occupied.\n");
}
}
int dequeue() {
if (count == 0) { // 缓冲区为空
printf("Buffer is empty. Unable to dequeue.\n");