This topic was mentioned in my CS data structure class, but I didn’t quit followed it. So wondering if anyone can spend a little time explain to me how that will work with sequential allocation in memory.
It will be very helpful with a graph or some illustration that can help me understand it.
Also I am wondering if this can happen to two queues.
Thank you
I am assuming sequential allocation used here means a block of memory, without gaps.
One possibility is that this can be something like a deque (double ended queue), except this would be a double ended stack. So for example, you allocate a block of sequential memory that has enough memory for N elements, you would have a pointer to the “top” of the first stack, which would start at the first element and grow to the right. You would also have a pointer to the “top” of the second stack, which would start at the last element and grow left. If the two “tops” cross each-other, its time to “grow” the memory; reallocate and copy one or both stacks to the new spot. Thus each of the stacks can grow until both of their sizes together surpasses N.
Illustration (from here, a page discussing using such data structures as allocators):
With respect to queues, yes you create two queues out of this block, but it will potentially have an extra hole/gap, as the bottoms of the queues can move up (away from the ends). A possible solution to this would be, instead of popping off the front, one could empty the front element, and swap with the back, while keeping pointers to the actual front and back.