Implementing a XOR Doubly Linked-List in C
After experimenting with the XOR swap method in Java I had hoped to follow it up with exploration of the XOR double linked-list. Java objects however, are not directly accessible through pointer reference. This is by no means a limitation, allowing the Garbage collector to efficiently and safely handle memory allocation. Sometimes it is desired to have such access however, and you can take advantage of JNI to call assembly, or C/C++ code from within Java. Another option is to use the sun.misc.Unsafe class, providing many ‘unsafe’ operations, one of which is to retrieve object memory address locations.
Despite these alternatives, I felt compelled to follow on with my exploration of C, and implement this data-structure using a language with great (scary) low-level memory-management support. From previous posts you are aware that a doubly linked-list stores items with pointers present in both directions. This allows for traversal both forwards and backwards, at the expense of having to store two pointers per element. Through the magic of the bitwise XOR operation however, we are able to store each nodes previous and next pointers in the memory allocated to just one.
Firstly, I would like to credit this post and this StackOverflow answer, which helped shape my implementation. Since in C we are unable to perform XOR operations on pointers directly, we must first convert them to integers using the ‘uintptr_t’ data type and then cast back, which can be seen in the ‘xor’ function. At its root the implementation works by storing the resulting XOR value from the previous and next pointer locations, per node. From this value we are then able to ‘undo’ the operation using the supplied known pointer, returning the other pointers value. If the node appears at the head or tail of the list, the known pointers value is XOR with 0 (NULL), in effect doing nothing. Below shows the different operations that are used to build-up and then traverse the list.
- previous ^ next = value (stored in node)
- value ^ previous = next (forward)
- value ^ next = previous (backward)
Looking at the ‘list’ method you can see that we are able to traverse the list (in either direction) by calculating the next pointer using the current value and the previous nodes value (starting with NULL). An issue with this implementation is that it only works if you start traversal from the head or tail, as you are required to know the previous pointers value. This takes away one of the big advantages of using a doubly linked-list, which is being able to remove any given node by its reference alone.