I wanted to implement a xor linked list in python and I searched for it to try understand it better but the only thing related to python that I found was this stackoverflow post How to implement an XOR Linked List in Python? that says that it's impossible to implement a xor linked list in python. It said there that you can't mess with bits and pointers. I think that we can actually 'mess with bits', using bitwise operators ( for xor we would have ^ ) and what does it mean by pointers ? We can create a node class with a pointer property like we would do in a singly linked list :
class Node(object):
def __init__(self, data):
self.data = data
self.next = None
And that would be our node with the 'pointer' -> .next. So, the question is, why can't we implement a XOR linked list in python and if we can, how ?
I could successfully implement an XOR linked list. Notice that in order to set a
Node's neighbors, you have to pass both the neighbors. If you don't do that, it is not possible to calculate the address using XOR operation (address_store = prev_address ^ curr_address).get_by_address(address)function will get you an object with a givenidat runtime, andNode.get_address(node)will get you aNode'sid. Clearly, it is possible to dereference an object in Python and also get its reference!Output: