본문 바로가기
C

C 언어 Ring Buffer(Circular Buffer)

by 가난한 농부 2023. 3. 24.

원형 버퍼(영어: circular buffer)는 고정된 크기의 버퍼를 양 끝이 연결된 것처럼 사용할 수 있게 해주는 자료 구조이다. 원형 버퍼를 이용하면 거의 성능 저하 없이 단순 배열 처럼 사용할 수 있다.

                                                                                                                                                                                                                             .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <stdio.h>
#include <stdlib.h>
 
#define BUFFER_SIZE 10
 
struct RingBuffer {
    int* buffer; // Pointer to the buffer
    int head; // Index of the head element
    int tail; // Index of the tail element
    int size// Current size of the buffer
};
 
// Initialize the ring buffer
void initRingBuffer(struct RingBuffer* rb) {
    rb->buffer = malloc(BUFFER_SIZE * sizeof(int));
    rb->head = 0;
    rb->tail = 0;
    rb->size = 0;
}
 
// Add an element to the ring buffer
void writeBuffer(struct RingBuffer* rb, int value) {
    if (rb->size == BUFFER_SIZE) {
        printf("Buffer is full!\n");
        return;
    }
 
    rb->buffer[rb->tail] = value;
    rb->tail = (rb->tail + 1) % BUFFER_SIZE;
    rb->size++;
}
 
// Remove an element from the ring buffer
int readBuffer(struct RingBuffer* rb) {
    if (rb->size == 0) {
        printf("Buffer is empty!\n");
        return -1;
    }
 
    int value = rb->buffer[rb->head];
    rb->head = (rb->head + 1) % BUFFER_SIZE;
    rb->size--;
    return value;
}
 
// Print the ring buffer
void printBuffer(struct RingBuffer* rb) {
    if (rb->size == 0) {
        printf("Buffer is empty!\n");
        return;
    }
 
    printf("Buffer contains: ");
    for (int i = 0; i < rb->size; i++) {
        int index = (rb->head + i) % BUFFER_SIZE;
        printf("%d ", rb->buffer[index]);
    }
    printf("\n");
}
 
int main() {
    struct RingBuffer rb;
    initRingBuffer(&rb);
 
    writeBuffer(&rb, 1);
    writeBuffer(&rb, 2);
    writeBuffer(&rb, 3);
    writeBuffer(&rb, 4);
    writeBuffer(&rb, 5);
 
    printBuffer(&rb);
 
    int value = readBuffer(&rb);
    printf("Removed value: %d\n", value);
 
    value = readBuffer(&rb);
    printf("Removed value: %d\n", value);
 
    printBuffer(&rb);
 
    writeBuffer(&rb, 6);
    writeBuffer(&rb, 7);
    writeBuffer(&rb, 8);
    writeBuffer(&rb, 9);
    writeBuffer(&rb, 10);
    writeBuffer(&rb, 1);
    writeBuffer(&rb, 2);
    writeBuffer(&rb, 3);
    writeBuffer(&rb, 4);
 
    printBuffer(&rb);
 
    value = readBuffer(&rb);
    printf("Removed value: %d\n", value);
 
    value = readBuffer(&rb);
    printf("Removed value: %d\n", value);
 
    printBuffer(&rb);
 
    writeBuffer(&rb, 5);
    writeBuffer(&rb, 6);
    writeBuffer(&rb, 7);
 
    printBuffer(&rb);
 
    return 0;
}
 
cs

 

'C' 카테고리의 다른 글

C 언어 오름차순 정렬, 내림차순 정렬  (0) 2023.03.24