From c4bb12a41044eeebba878b424ca59c46f30cb1b1 Mon Sep 17 00:00:00 2001 From: Speedster3030 Date: Sat, 13 Sep 2025 18:01:52 +0530 Subject: [PATCH 1/2] Ring Buffer/Circular Queue implementation added --- data_structures/ringbuffer.c | 129 +++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 data_structures/ringbuffer.c diff --git a/data_structures/ringbuffer.c b/data_structures/ringbuffer.c new file mode 100644 index 0000000000..41681107b1 --- /dev/null +++ b/data_structures/ringbuffer.c @@ -0,0 +1,129 @@ +/** + * Speedster3030, 2025 + * Ring Buffer/Circular Queue structure implementation. + */ + +#include +#include + +//ring Buffer/Circular Queue struct +typedef struct +{ + int* arr; + int count; + int head; + int tail; + int max; +}ringBuffer; + +//Initialize a new ring buffer +ringBuffer* new_ringBuffer(int size) +{ + ringBuffer* rb=malloc(sizeof(ringBuffer)); + + rb->max=size; + rb->arr=malloc(size*sizeof(int)); + rb->count=0; + rb->head=size-1; + rb->tail=size-1; + + return rb; +} + +//free the structure from memory +void delete_Buffer(ringBuffer **rb) +{ + free((*rb)->arr); + free(*rb); + *rb=NULL; +} + +//Put a value in the buffer +int enqueue(ringBuffer *rb,int n) +{ + if(rb->count==rb->max) + { + return -1; + } + + rb->arr[rb->tail]=n; + rb->tail--; + if(rb->tail==-1) + { + rb->tail=rb->max-1; + } + rb->count++; + return 0; +} + +//remove a value from the buffer +int dequeue(ringBuffer *rb,int *result) +{ + if(rb->count==0) + { + return -1; + } + + int t=rb->arr[rb->head]; + rb->arr[rb->head]=0; + rb->head--; + rb->count--; + if(rb->head==-1) + { + rb->head=rb->max-1; + } + + *result=t; + return 0; +} + +//get the value in front of the queue +int front(ringBuffer *rb,int *result) +{ + if(rb->count==0) + { + return -1; + } + + *result=rb->arr[rb->head]; + return 0; +} + +int isEmpty(ringBuffer *rb) +{ + return rb->count==0?1:0; +} + +int size(ringBuffer *rb) +{ + return rb->count; +} + +//basic display for the queue +void display(ringBuffer *rb) +{ + if(rb->count==0) + { + printf("Empty Ring Buffer\n"); + return; + } + + printf("\nQueue: "); + + int c=rb->head; + int count=rb->count; + while(count>0) + { + printf("%d, ",rb->arr[c]); + c--; + if(c<0) + { + c=rb->max-1; + } + count--; + } + + printf("\n"); +} + + From a0af75889e54b8348d3ba8ff5f0a1f5cf20e1e21 Mon Sep 17 00:00:00 2001 From: Speedster3030 Date: Sat, 13 Sep 2025 23:48:26 +0530 Subject: [PATCH 2/2] Made suggested improvements,added test file --- data_structures/{ => ringbuffer}/ringbuffer.c | 77 +++++++++++-------- data_structures/ringbuffer/ringbuffer.h | 26 +++++++ data_structures/ringbuffer/test.c | 24 ++++++ 3 files changed, 94 insertions(+), 33 deletions(-) rename data_structures/{ => ringbuffer}/ringbuffer.c (67%) create mode 100644 data_structures/ringbuffer/ringbuffer.h create mode 100644 data_structures/ringbuffer/test.c diff --git a/data_structures/ringbuffer.c b/data_structures/ringbuffer/ringbuffer.c similarity index 67% rename from data_structures/ringbuffer.c rename to data_structures/ringbuffer/ringbuffer.c index 41681107b1..1d0657251b 100644 --- a/data_structures/ringbuffer.c +++ b/data_structures/ringbuffer/ringbuffer.c @@ -1,20 +1,26 @@ /** - * Speedster3030, 2025 + * Speedster3030; 13 Sept,2025 * Ring Buffer/Circular Queue structure implementation. */ -#include -#include +#include +#include +#include "ringbuffer.h" + +#define RB_OK 0 +#define RB_FULL -1 +#define RB_EMPTY -2 //ring Buffer/Circular Queue struct -typedef struct +struct ringBuffer { int* arr; int count; int head; int tail; int max; -}ringBuffer; +}; + //Initialize a new ring buffer ringBuffer* new_ringBuffer(int size) @@ -24,12 +30,13 @@ ringBuffer* new_ringBuffer(int size) rb->max=size; rb->arr=malloc(size*sizeof(int)); rb->count=0; - rb->head=size-1; - rb->tail=size-1; + rb->head=0; + rb->tail=-1; return rb; } + //free the structure from memory void delete_Buffer(ringBuffer **rb) { @@ -38,67 +45,77 @@ void delete_Buffer(ringBuffer **rb) *rb=NULL; } + //Put a value in the buffer int enqueue(ringBuffer *rb,int n) { if(rb->count==rb->max) { - return -1; + return RB_FULL; } + rb->tail=(rb->tail+1) % rb->max; rb->arr[rb->tail]=n; - rb->tail--; - if(rb->tail==-1) - { - rb->tail=rb->max-1; - } rb->count++; - return 0; + return RB_OK; } + //remove a value from the buffer int dequeue(ringBuffer *rb,int *result) { if(rb->count==0) { - return -1; + return RB_EMPTY; } - int t=rb->arr[rb->head]; + *result=rb->arr[rb->head]; rb->arr[rb->head]=0; - rb->head--; + rb->head=(rb->head+1) % rb->max; rb->count--; - if(rb->head==-1) - { - rb->head=rb->max-1; - } - *result=t; - return 0; + return RB_OK; } + //get the value in front of the queue int front(ringBuffer *rb,int *result) { if(rb->count==0) { - return -1; + return RB_EMPTY; } *result=rb->arr[rb->head]; - return 0; + return RB_OK; +} + + +//get the rear value of the queue +int rear(ringBuffer *rb,int *result) +{ + if(rb->count==0) + { + return RB_EMPTY; + } + + *result=rb->arr[rb->tail]; + return RB_OK; } + int isEmpty(ringBuffer *rb) { return rb->count==0?1:0; } + int size(ringBuffer *rb) { return rb->count; } + //basic display for the queue void display(ringBuffer *rb) { @@ -115,15 +132,9 @@ void display(ringBuffer *rb) while(count>0) { printf("%d, ",rb->arr[c]); - c--; - if(c<0) - { - c=rb->max-1; - } + c=(c+1)%rb->max; count--; } - printf("\n"); + printf("\n\n"); } - - diff --git a/data_structures/ringbuffer/ringbuffer.h b/data_structures/ringbuffer/ringbuffer.h new file mode 100644 index 0000000000..8f422f8f85 --- /dev/null +++ b/data_structures/ringbuffer/ringbuffer.h @@ -0,0 +1,26 @@ + +#ifndef RINGBUFFER_H +#define RINGBUFFER_H + + +typedef struct ringBuffer ringBuffer; + +ringBuffer* new_ringBuffer(int size); +void delete_Buffer(ringBuffer **rb); + + +int enqueue(ringBuffer *rb,int n); +int dequeue(ringBuffer *rb,int *result); + + +int front(ringBuffer *rb,int *result); +int rear(ringBuffer *rb,int *result); + + +int isEmpty(ringBuffer *rb); +int size(ringBuffer *rb); + + +void display(ringBuffer *rb); + +#endif diff --git a/data_structures/ringbuffer/test.c b/data_structures/ringbuffer/test.c new file mode 100644 index 0000000000..81b1d9647e --- /dev/null +++ b/data_structures/ringbuffer/test.c @@ -0,0 +1,24 @@ + +#include +#include +#include "ringbuffer.h" + +int main() +{ + ringBuffer* r=new_ringBuffer(4); + int result; + + enqueue(r,1); + enqueue(r,2); + enqueue(r,3); + enqueue(r,4); + display(r); + dequeue(r,&result); + display(r); + rear(r,&result); + printf("%d\n",result); + + delete_Buffer(&r); + + return 0; +}