Skip to content
Open
Changes from 1 commit
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
129 changes: 129 additions & 0 deletions data_structures/ringbuffer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
* Speedster3030, 2025
* Ring Buffer/Circular Queue structure implementation.
*/

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

//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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might consider macro definitions. Would make the return values more for readability

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

}

rb->arr[rb->tail]=n;
rb->tail--;
if(rb->tail==-1)
{
rb->tail=rb->max-1;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider more conventional style for circular queues:

Suggested change
rb->arr[rb->tail]=n;
rb->tail--;
if(rb->tail==-1)
{
rb->tail=rb->max-1;
}
rb->arr[rb->tail] = n;
rb->tail = (rb->tail + 1) % rb->max;

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;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider more conventional style for circular queues:

Suggested change
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 = rb->arr[rb->head];
rb->head = (rb->head + 1) % rb->max;


*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;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You already provide a front() function, but no equivalent rear() function. Since the buffer tracks both ends, it would be useful (and consistent with Circular Queue implementations) to add a rear() to peek at the last enqueued element.

Suggested change
// get the value of the rear of queue
int rear(ringBuffer *rb, int *result)
{
if (rb->count == 0)
return -1;
int idx = (rb->tail + 1) % rb->max;
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");
}