Complete the code to declare the ring buffer size constant.
const int BUFFER_SIZE = [1];The ring buffer size is set as a constant integer value. Here, 128 is a common buffer size.
Complete the code to declare the ring buffer array.
char ring_buffer[[1]];The ring buffer array is declared with the size constant BUFFER_SIZE.
Fix the error in the function to check if the ring buffer is empty.
int is_empty() {
return [1] == tail;
}The ring buffer is empty when the head index equals the tail index.
Fill both blanks to correctly advance the head index in the ring buffer.
head = (head [1] 1) [2] BUFFER_SIZE;
The head index is incremented by 1 and wrapped around using modulo BUFFER_SIZE.
Fill all three blanks to implement the function that adds a character to the ring buffer safely.
int enqueue(char c) {
if ((head [1] 1) [2] BUFFER_SIZE == tail) {
return 0; // Buffer full
}
ring_buffer[head] = c;
head = (head [3] 1) % BUFFER_SIZE;
return 1;
}The function checks if the buffer is full by seeing if incrementing head wraps to tail. It uses addition and modulo to update the head index safely.