-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Ring Buffer/Circular Queue implementation added #1482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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.
data_structures/ringbuffer.c
Outdated
{ | ||
if(rb->count==rb->max) | ||
{ | ||
return -1; |
There was a problem hiding this comment.
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;
data_structures/ringbuffer.c
Outdated
rb->arr[rb->tail]=n; | ||
rb->tail--; | ||
if(rb->tail==-1) | ||
{ | ||
rb->tail=rb->max-1; | ||
} |
There was a problem hiding this comment.
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:
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; |
data_structures/ringbuffer.c
Outdated
int t=rb->arr[rb->head]; | ||
rb->arr[rb->head]=0; | ||
rb->head--; | ||
rb->count--; | ||
if(rb->head==-1) | ||
{ | ||
rb->head=rb->max-1; | ||
} |
There was a problem hiding this comment.
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:
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; | ||
} | ||
|
There was a problem hiding this comment.
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.
// 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; | |
} | |
Ok, made it more intuitive and made a header file along with a test file |
There was a problem hiding this 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!
Um sorry for the second request i clicked it by accident |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🇨🚀
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
Notes:
A Basic Ring Buffer Implementation In C, in ringbuffer directory added to C/data_structures/, added suggested improvements