I recently came across a good approach to this problem in a book by Nicklaus Wirth ("Algorithms + Data Structures = Programs"). It looks like this ... You have a node similar to the one you proposed, except that it does not aggregate a pointer to the next (or previous) Node . Instead, you have a single link member that represents the distance (for example, in units of sizeof(Node) ) from the previous node (which Node* pPrev ) in the chain to the next node (which Node* pNext ) in the chain:
size_t link = pNext - pPrev;
So node might look something like this:
struct Node { int val; size_t link; }
Then, to move from the current node, pCurrent , in combination with the previous node pPrev , to the next Node* pNext , writing:
pNext = pPrev + pCurrent->link;
Similarly, you can move in the opposite direction by rearranging this equation:
pPrev = pNext - pCurrent->link;
However, this approach is somewhat limited by the arithmetic of pointers to C / C ++, since the difference of two pointers is only well defined if both points are inside the same memory block. In fact, all your nodes should be contained within one huge array of Node s.
Julian
source share