Create a linked list without pointers but indices

advertisements

This question already has an answer here:

  • Is it possible to create a single linked list with indexes but without pointers? [duplicate] 1 answer

My teacher wants me to create something like linked list but using indices instead of pointers. So the Node contains int data and int index.

Can you drop me a hint how would I do that? I know how to create it with pointers but how to do without them? He mentioned pool as a container though.


Your struct Node would be like this

struct Node {
    int data;
    int index; // index of the entry in the pool to the next node
}

You can use a vector or array to create a pool

vector<Node*> pool; // stores the pointer to next nodes

Now to access the next node you can do

Node* nextNode = pool[currNode->index];