parent
c1b3b9a9d6
commit
42aeecf17a
|
@ -34,15 +34,18 @@ class XorList:
|
||||||
def insert(self, data):
|
def insert(self, data):
|
||||||
node = Node(data)
|
node = Node(data)
|
||||||
node_id = id(node)
|
node_id = id(node)
|
||||||
# New node npx is XOR of current head and NULL
|
# New node npx is XOR of current head and id(None)
|
||||||
node.npx = id(self.head)
|
|
||||||
# node.npx = self.xor(node_id, id(self.head))
|
# node.npx = self.xor(node_id, id(self.head))
|
||||||
|
node.npx = id(self.head)
|
||||||
|
|
||||||
if self.head is not None:
|
if self.head is None:
|
||||||
|
# list is empty, set tail to the first node.
|
||||||
|
self.tail = node
|
||||||
|
else: # list is not empty, update npx of old head
|
||||||
old_head_npx = self.head.npx
|
old_head_npx = self.head.npx
|
||||||
self.head.npx = self.xor(node_id, old_head_npx)
|
self.head.npx = self.xor(node_id, old_head_npx)
|
||||||
else:
|
# old_head_id = id(self.head)
|
||||||
self.tail = node
|
# node.npx = self.xor(old_head_id, id(None))
|
||||||
|
|
||||||
self.__nodes.append(node)
|
self.__nodes.append(node)
|
||||||
self.head = node
|
self.head = node
|
||||||
|
@ -67,7 +70,7 @@ class XorList:
|
||||||
|
|
||||||
def backward(self):
|
def backward(self):
|
||||||
print("backward visit list")
|
print("backward visit list")
|
||||||
rear_id = NULL
|
rear_id = id(None)
|
||||||
curr_node = self.tail
|
curr_node = self.tail
|
||||||
|
|
||||||
if curr_node is None:
|
if curr_node is None:
|
||||||
|
@ -85,7 +88,10 @@ class XorList:
|
||||||
"""
|
"""
|
||||||
Returns a new instance of type points to the same memory
|
Returns a new instance of type points to the same memory
|
||||||
"""
|
"""
|
||||||
return ctypes.cast(obj_id, ctypes.py_object).value
|
py_obj = ctypes.cast(obj_id, ctypes.py_object)
|
||||||
|
if not py_obj:
|
||||||
|
return None
|
||||||
|
return py_obj.value
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in New Issue