Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions data_structures/ringbuffer/ringbuffer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/**
* Speedster3030; 13 Sept,2025
* Ring Buffer/Circular Queue structure implementation.
*/

#include <stdio.h>
#include <stdlib.h>
#include "ringbuffer.h"

#define RB_OK 0
#define RB_FULL -1
#define RB_EMPTY -2

//ring Buffer/Circular Queue struct
struct ringBuffer
{
int* arr;
int count;
int head;
int tail;
int max;
};


//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=0;
rb->tail=-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 RB_FULL;
}

rb->tail=(rb->tail+1) % rb->max;
rb->arr[rb->tail]=n;
rb->count++;
return RB_OK;
}


//remove a value from the buffer
int dequeue(ringBuffer *rb,int *result)
{
if(rb->count==0)
{
return RB_EMPTY;
}

*result=rb->arr[rb->head];
rb->arr[rb->head]=0;
rb->head=(rb->head+1) % rb->max;
rb->count--;

return RB_OK;
}


//get the value in front of the queue
int front(ringBuffer *rb,int *result)
{
if(rb->count==0)
{
return RB_EMPTY;
}

*result=rb->arr[rb->head];
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)
{
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=(c+1)%rb->max;
count--;
}

printf("\n\n");
}
26 changes: 26 additions & 0 deletions data_structures/ringbuffer/ringbuffer.h
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions data_structures/ringbuffer/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

#include <stdio.h>
#include <stdlib.h>
#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;
}