-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathringbuf.c
More file actions
115 lines (97 loc) · 2.16 KB
/
ringbuf.c
File metadata and controls
115 lines (97 loc) · 2.16 KB
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
110
111
112
113
114
115
//基于数组设计一个环形缓冲区,定长为N的FIFO对了
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct ringbuf {
int *data;
int max;
int head;
int tail;
int count;
};
struct ringbuf buf;
void print_ringbuf(struct ringbuf *buf)
{
printf("max:%d head:%d tail:%d count:%d \n",
buf->max, buf->head, buf->tail, buf->count);
}
int buffer_init(struct ringbuf *buf, int size)
{
buf->max = size + 1;
buf->head = 0;
buf->tail = 0;
buf->count = 0;
buf->data = (int *)malloc(buf->max * sizeof(int));
if (buf->data == NULL)
return -1;
print_ringbuf(buf);
return 0;
}
int empty(struct ringbuf *buf)
{
if (buf->head == buf->tail)
return 1;
else
return 0;
}
int full(struct ringbuf *buf)
{
if (buf->count == buf->max - 1)
return 1;
else
return 0;
}
void increase_head(struct ringbuf *buf)
{
buf->head = (buf->head + 1) % buf->max;
if (buf->tail >= buf->head)
buf->count = buf->tail - buf->head;
else
buf->count = buf->tail + buf->max - buf->head;
}
void increase_tail(struct ringbuf *buf)
{
buf->tail = (buf->tail + 1) % buf->max;
if (full(buf))
buf->head = (buf->head + 1) % buf->max;
if (buf->tail >= buf->head)
buf->count = buf->tail - buf->head;
else
buf->count = buf->tail + buf->max - buf->head;
}
int take(struct ringbuf *buf)
{
int value;
if (empty(buf))
return -1;
value = buf->data[buf->head];
increase_head(buf);
print_ringbuf(buf);
return value;
}
bool put(struct ringbuf *buf, int value)
{
buf->data[buf->tail] = value;
increase_tail(buf);
print_ringbuf(buf);
return true;
}
int main(void)
{
char command;
int value;
while(1) {
scanf("%c", &command);
if (command == 'p') { //put
scanf("%d", &value);
put(&buf, value);
} else if (command == 't'){ //take
value = take(&buf);
printf("Take:%d\n", value);
} else if (command == 'c') { //create
scanf("%d", &value);
buffer_init(&buf, value);
}
}
return 0;
}