XOR Linked List

An XOR linked list is a data structure used in computer programming. It takes advantage of the bitwise exclusive disjunction (XOR) operation, here denoted by ⊕, to decrease storage requirements for doubly linked lists. An ordinary doubly linked list stores addresses of the previous and next list items in each list node, requiring two address fields:

... A B C D E ... –> next –> next –> next –> <– prev <– prev <– prev <–

An XOR linked list compresses the same information into one address field by storing the bitwise XOR of the address for previous and the address for next in one field:

... A B C D E ... <–> A⊕C <-> B⊕D <-> C⊕E <->

When you traverse the list from left to right: supposing you are at C, you can take the address of the previous item, B, and XOR it with the value in the link field (B⊕D). You will then have the address for D and you can continue traversing the list. The same pattern applies in the other direction.

To start traversing the list in either direction from some point, you need the address of two consecutive items, not just one. If the addresses of the two consecutive items are reversed, you will end up traversing the list in the opposite direction.

This form of linked list may be inadvisable:

  • General-purpose debugging tools cannot follow the XOR chain, making debugging more difficult;
  • The price for the decrease in memory usage is an increase in code complexity, making maintenance more expensive;
  • Most garbage collection schemes do not work with data structures that do not contain literal pointers;
  • XOR of pointers is not defined in some contexts (e.g., the C language), although many languages provide some kind of type conversion between pointers and integers;
  • The pointers will be unreadable if one isn't traversing the list — for example, if the pointer to a list item was contained in another data structure;
  • While traversing the list you need to remember the address of the previously accessed node in order to calculate the next node's address.

Computer systems have increasingly cheap and plentiful memory, and storage overhead is not generally an overriding issue outside specialized embedded systems. Where it is still desirable to reduce the overhead of a linked list, unrolling provides a more practical approach (as well as other advantages, such as increasing cache performance and speeding random access).

Read more about XOR Linked List:  Features, Why Does It Work?, Variations

Famous quotes containing the words linked and/or list:

    In the dominant Western religious system, the love of God is essentially the same as the belief in God, in God’s existence, God’s justice, God’s love. The love of God is essentially a thought experience. In the Eastern religions and in mysticism, the love of God is an intense feeling experience of oneness, inseparably linked with the expression of this love in every act of living.
    Erich Fromm (1900–1980)

    Lastly, his tomb
    Shall list and founder in the troughs of grass
    And none shall speak his name.
    Karl Shapiro (b. 1913)