Problem understanding XOR linked list

I am reading an XOR linked list (from Wikipedia). But I have some problems in his understanding.

I do not get the next paragraph.

To start moving the list in any direction from some point, you need the address of two consecutive elements, not just one. If the addresses of two consecutive elements are canceled, you will go to the list in the opposite direction.

I have a few questions about this:

  • How does it work (XOR linked list itself)?

    (It would be great if you justified your answer by giving an example.ie example by accepting some addresses and then doing some calculations accordingly.)

  • How can I implement it? A brief description of the implementation.

  • Practically, where is it or can I use it? Is it really useful as it seems?
+3
source share
2 answers
  • The above procedure works by storing the addresses of both the next and previous elements in one field (let it call A). Therefore, to get the value of the next element in the list, you take the address of the other element that you have (so you need two ... call it B). To find the address of the next element, you simply A XOR B to get C (the location of the next element).

  • Depends on which language you use. Learn bitwise operations in any language you use, and you need to quickly understand it.

  • , ( XOR ). , .

+5

3. . - , . , , :

:

  • XOR, ;
  • - , ;
  • , ;
  • XOR (, C), ;
  • , - , ;
  • node, node.

, . , ( , ).

+5

Source: https://habr.com/ru/post/1721088/


All Articles