Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Speedster3030 wants to merge2 commits intoTheAlgorithms:master
base:master
Choose a base branch
Loading
fromSpeedster3030:master

Conversation

Speedster3030
Copy link

@Speedster3030Speedster3030 commentedSep 13, 2025
edited
Loading

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 matchesFile name guidelines
  • Added tests and example, test must pass
  • Relevant documentation/comments is changed or added
  • PR title follows semanticcommit 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

@richvigoritorichvigorito 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 revisitedGeeksforGeeks
andTutorialsPoint
). 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
intt=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 withCircular Queue implementations) to add a rear() to peek at the last enqueued element.

Suggested change
// get the value of the rear of queue
intrear(ringBuffer*rb,int*result)
{
if (rb->count==0)
return-1;
intidx= (rb->tail+1) %rb->max;
return0;
}

@Speedster3030
Copy link
Author

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

Copy link

@richvigoritorichvigorito left a comment
edited
Loading

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

@richvigoritorichvigorito left a comment

Choose a reason for hiding this comment

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

🇨🚀

Speedster3030 reacted with heart emoji
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@Panquesito7Panquesito7Awaiting requested review from Panquesito7Panquesito7 is a code owner

@tjgurwara99tjgurwara99Awaiting requested review from tjgurwara99tjgurwara99 is a code owner

@alexpantyukhinalexpantyukhinAwaiting requested review from alexpantyukhinalexpantyukhin is a code owner

1 more reviewer

@richvigoritorichvigoritorichvigorito approved these changes

Reviewers whose approvals may not affect merge requirements

At least 1 approving review is required to merge this pull request.

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@Speedster3030@richvigorito

[8]ページ先頭

©2009-2025 Movatter.jp