I've got a question if it's possible to do that?
One man said to me that i can do something like this:
struct Node
{
int data;
int NextIndex;
}
But I am not sure if I should not use pointers at all.
I've got a question if it's possible to do that?
One man said to me that i can do something like this:
struct Node
{
int data;
int NextIndex;
}
But I am not sure if I should not use pointers at all.
I've done that many times (I work in embedded systems, so I use tricks like this). The advantage of using indexes is that an index uses less memory than a pointer (usually a uint8_t or uint16_t), so if memory is a concern, you can do that.
You are limited to using a single array in that case (because the index has to be relative to that). Because you are not doing dynamic allocation, in this case, insertion and deletion is very quick. Notice though that you can use pointers in an array as well, and if you're optimizing for time, then this is even faster than indexes (on most architectures I've worked on anyways).
As far as using the next pointers, you allocate a large array, and create a free
head which points to the first element, and then make all elements point to the next one, so essentially you have a free-linked-list. If you want to use an element, you pop it from the free list, and push it onto the new list you wish to use. When you free an element, you simply push it back onto the free list.