-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathring.cpp
66 lines (60 loc) · 1.17 KB
/
ring.cpp
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
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 4
typedef int t_Elmt;
typedef struct ring{
t_Elmt data[MAX_SIZE];
int head;
int rear;
}s_ring;
void initRing(s_ring *rg){
rg->head = 0;
rg->rear = 0;
}
bool insertRing(s_ring *rg, t_Elmt e){
if ((rg->rear + 1)%MAX_SIZE == rg->head){
printf("end of ring \n");
return false;
}
rg->data[rg->rear] = e;
//printf("insert %d \n", rg->data[rg->rear]);
rg->rear = (rg->rear+1)%MAX_SIZE;
//printf("rear %d \n", rg->rear);
return true;
}
bool deleteRing(s_ring*rg){
if(rg->head == rg->rear){
printf("empty ring \n");
return false;
}
rg->head = (rg->head+1)%MAX_SIZE;
return true;
}
int getLen(s_ring *rg){
if (rg->rear >= rg->head)
return (rg->rear - rg->head);
else
return (MAX_SIZE - rg->head + rg->rear);
}
void peekRing(s_ring *rg){
int len = getLen(rg);
printf("len is %d \n ", len);
for(int i=0; i<len; i++)
printf(" %d ", rg->data[rg->head+i%MAX_SIZE]);
printf(" \n ");
}
int main(){
s_ring rg;
initRing(&rg);
insertRing(&rg, 1);
peekRing(&rg);
insertRing(&rg, 2);
peekRing(&rg);
insertRing(&rg, 3);
peekRing(&rg);
deleteRing(&rg);
peekRing(&rg);
insertRing(&rg, 4);
peekRing(&rg);
return 1;
}