-
-
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?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
rb->arr[rb->tail]=n; | ||||||||||||||||||||||||||
rb->tail--; | ||||||||||||||||||||||||||
if(rb->tail==-1) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
rb->tail=rb->max-1; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
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; |
Outdated
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; | |
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; | |
} | |
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;