Skip to content

Commit a0af758

Browse files
committed
Made suggested improvements,added test file
1 parent c4bb12a commit a0af758

File tree

3 files changed

+94
-33
lines changed

3 files changed

+94
-33
lines changed

data_structures/ringbuffer.c renamed to data_structures/ringbuffer/ringbuffer.c

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
/**
2-
* Speedster3030, 2025
2+
* Speedster3030; 13 Sept,2025
33
* Ring Buffer/Circular Queue structure implementation.
44
*/
55

6-
#include<stdio.h>
7-
#include<stdlib.h>
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include "ringbuffer.h"
9+
10+
#define RB_OK 0
11+
#define RB_FULL -1
12+
#define RB_EMPTY -2
813

914
//ring Buffer/Circular Queue struct
10-
typedef struct
15+
struct ringBuffer
1116
{
1217
int* arr;
1318
int count;
1419
int head;
1520
int tail;
1621
int max;
17-
}ringBuffer;
22+
};
23+
1824

1925
//Initialize a new ring buffer
2026
ringBuffer* new_ringBuffer(int size)
@@ -24,12 +30,13 @@ ringBuffer* new_ringBuffer(int size)
2430
rb->max=size;
2531
rb->arr=malloc(size*sizeof(int));
2632
rb->count=0;
27-
rb->head=size-1;
28-
rb->tail=size-1;
33+
rb->head=0;
34+
rb->tail=-1;
2935

3036
return rb;
3137
}
3238

39+
3340
//free the structure from memory
3441
void delete_Buffer(ringBuffer **rb)
3542
{
@@ -38,67 +45,77 @@ void delete_Buffer(ringBuffer **rb)
3845
*rb=NULL;
3946
}
4047

48+
4149
//Put a value in the buffer
4250
int enqueue(ringBuffer *rb,int n)
4351
{
4452
if(rb->count==rb->max)
4553
{
46-
return -1;
54+
return RB_FULL;
4755
}
4856

57+
rb->tail=(rb->tail+1) % rb->max;
4958
rb->arr[rb->tail]=n;
50-
rb->tail--;
51-
if(rb->tail==-1)
52-
{
53-
rb->tail=rb->max-1;
54-
}
5559
rb->count++;
56-
return 0;
60+
return RB_OK;
5761
}
5862

63+
5964
//remove a value from the buffer
6065
int dequeue(ringBuffer *rb,int *result)
6166
{
6267
if(rb->count==0)
6368
{
64-
return -1;
69+
return RB_EMPTY;
6570
}
6671

67-
int t=rb->arr[rb->head];
72+
*result=rb->arr[rb->head];
6873
rb->arr[rb->head]=0;
69-
rb->head--;
74+
rb->head=(rb->head+1) % rb->max;
7075
rb->count--;
71-
if(rb->head==-1)
72-
{
73-
rb->head=rb->max-1;
74-
}
7576

76-
*result=t;
77-
return 0;
77+
return RB_OK;
7878
}
7979

80+
8081
//get the value in front of the queue
8182
int front(ringBuffer *rb,int *result)
8283
{
8384
if(rb->count==0)
8485
{
85-
return -1;
86+
return RB_EMPTY;
8687
}
8788

8889
*result=rb->arr[rb->head];
89-
return 0;
90+
return RB_OK;
91+
}
92+
93+
94+
//get the rear value of the queue
95+
int rear(ringBuffer *rb,int *result)
96+
{
97+
if(rb->count==0)
98+
{
99+
return RB_EMPTY;
100+
}
101+
102+
*result=rb->arr[rb->tail];
103+
return RB_OK;
90104
}
91105

106+
92107
int isEmpty(ringBuffer *rb)
93108
{
94109
return rb->count==0?1:0;
95110
}
96111

112+
97113
int size(ringBuffer *rb)
98114
{
99115
return rb->count;
100116
}
101117

118+
102119
//basic display for the queue
103120
void display(ringBuffer *rb)
104121
{
@@ -115,15 +132,9 @@ void display(ringBuffer *rb)
115132
while(count>0)
116133
{
117134
printf("%d, ",rb->arr[c]);
118-
c--;
119-
if(c<0)
120-
{
121-
c=rb->max-1;
122-
}
135+
c=(c+1)%rb->max;
123136
count--;
124137
}
125138

126-
printf("\n");
139+
printf("\n\n");
127140
}
128-
129-
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
#ifndef RINGBUFFER_H
3+
#define RINGBUFFER_H
4+
5+
6+
typedef struct ringBuffer ringBuffer;
7+
8+
ringBuffer* new_ringBuffer(int size);
9+
void delete_Buffer(ringBuffer **rb);
10+
11+
12+
int enqueue(ringBuffer *rb,int n);
13+
int dequeue(ringBuffer *rb,int *result);
14+
15+
16+
int front(ringBuffer *rb,int *result);
17+
int rear(ringBuffer *rb,int *result);
18+
19+
20+
int isEmpty(ringBuffer *rb);
21+
int size(ringBuffer *rb);
22+
23+
24+
void display(ringBuffer *rb);
25+
26+
#endif

data_structures/ringbuffer/test.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include "ringbuffer.h"
5+
6+
int main()
7+
{
8+
ringBuffer* r=new_ringBuffer(4);
9+
int result;
10+
11+
enqueue(r,1);
12+
enqueue(r,2);
13+
enqueue(r,3);
14+
enqueue(r,4);
15+
display(r);
16+
dequeue(r,&result);
17+
display(r);
18+
rear(r,&result);
19+
printf("%d\n",result);
20+
21+
delete_Buffer(&r);
22+
23+
return 0;
24+
}

0 commit comments

Comments
 (0)