Implement a Queue with a single buffer without allocating more memory

Hi!

I'm trying to create a queue with a single dynamic allocated buffer, which means that I don't want to allocate more memory for temporary variables, etc. The queue will be of strings.

So, I was thinking at a strategy and what do I need to keep track of.

I think I need 1 "begin" and 2 "end" pointers, because I may have two ranges in this buffer.

So, I may have a situation as follows

1
2
3
       E2    B1       E1
|xxxxxxx|    |xxxxxxxxx|    |


where x are characters of a string. We have these two ranges because one ore more words that a user enqueued didn't fit between E1 and the end of the buffer, so we go in circle.

I was wondering what do you think about this approach, apart from the fact that this will lead not to the best use of this buffer, but at least we don't have to allocate intermediary buffers and deallocate them immediately after.
Last edited on
so its like circular queue with two (different) ends?

this is great and interesting....

could you show us the code to implement this???

so its like circular queue with two (different) ends?

Yes...


this is great and interesting....


I guess so...


could you show us the code to implement this???


I finished implementing it now (using C). I will put the code on one of my Github's repositories. I will tell you in the moment the link to it...
Last edited on
could you provide your github link.... I would love to see your other projects too
could you provide your github link.... I would love to see your other projects too


Here you have the link to the strqueue module I've just implemented. If you find a bug, open an issue directly on the Github repository, please!

https://github.com/dossan/c-exercises/tree/master/strqueue
thank you
Topic archived. No new replies allowed.