Skip to content

Conversation

Speedster3030
Copy link

@Speedster3030 Speedster3030 commented Sep 13, 2025

Description of Change

I have added a directory ringbuffer in C/data_structures/ which contains ringbuffer.c, ringbuffer.h, and test.c
of a ring buffer/Circular Queue in C;

References

Checklist

  • Added description of change
  • Added file name matches File name guidelines
  • Added tests and example, test must pass
  • Relevant documentation/comments is changed or added
  • PR title follows semantic commit guidelines
  • Search previous suggestions before making a new one, as yours may be a duplicate.
  • I acknowledge that all my contributions will be made under the project's license.

Notes:
A Basic Ring Buffer Implementation In C, in ringbuffer directory added to C/data_structures/, added suggested improvements

Copy link

@richvigorito richvigorito left a comment

Choose a reason for hiding this comment

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

Thanks for sharing this implementation! 🎉

I’ll admit this PR sent me back to refresh circular queue algorithms (I revisited GeeksforGeeks
and TutorialsPoint
). After doing that, here are my thoughts:

  • The API is clear and works correctly.
  • The head/tail indexing works, but it’s a bit unconventional (decrementing instead of incrementing). A (x + 1) % max style is more common and may be easier to follow.
  • front() is great, but you might also consider adding a rear() function for API completeness.
  • Minor style: define macro definitions so return values are easier to digest when reading.

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

Comment on lines 49 to 54
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;

Comment on lines 67 to 74
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=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;
}

@Speedster3030
Copy link
Author

Ok, made it more intuitive and made a header file along with a test file

Copy link

@richvigorito richvigorito left a comment

Choose a reason for hiding this comment

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

Looks great. Was fun researching this data structure!

@Speedster3030
Copy link
Author

Um sorry for the second request i clicked it by accident

Copy link

@richvigorito richvigorito left a comment

Choose a reason for hiding this comment

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

🇨🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants