A dedicated
reverse_list
method is not really necessary.An
__iter__
method is more useful than a dedicatedprint_list
method (thanks ChatterOne ChatterOne).Circular lists are also reversed neatly this way (note that
__iter__
cycles endlessly for circular lists).
A dedicated
reverse_list
method is not really necessary.An
__iter__
method is more useful than a dedicatedprint_list
method (thanks ChatterOne).Circular lists are also reversed neatly this way (note that
__iter__
cycles endlessly for circular lists).
A dedicated
reverse_list
method is not really necessary.An
__iter__
method is more useful than a dedicatedprint_list
method (thanks ChatterOne).Circular lists are also reversed neatly this way (note that
__iter__
cycles endlessly for circular lists).
class DoubleLinkedListNode:
def __init__(self, value, plus_node, dir=None):
self.value = value
self.minus_node = None
if plus_node:
plus_node.minus_node = self
if dir == None:
self.dir = plus_node.dir
if dir != None:
self.dir = dir
self.plus_node = plus_node
def __iter__(self):
self.cur = self
return self
def __next__(self):
if self.cur:
current = self.cur
if self.cur.dir:
if self.cur.dir[0] > 0:
self.cur = self.cur.plus_node
elif self.cur.dir[0] < 0:
self.cur = self.cur.minus_node
return current
else:
raise StopIteration
if __name__ == "__main__":
tail = DoubleLinkedListNode(5, None, [1]) # [1] sets direction for all nodes in list.
head = DoubleLinkedListNode(1, DoubleLinkedListNode(2, DoubleLinkedListNode(3, DoubleLinkedListNode(4, tail))))
for i in head: print(i.value) # Prints node values forwards from head.
head.dir[0] = -1 # reversesReverses direction for all nodes in the list.
for i in tail: print(i.value) # Prints node values in reverse from tail.
class DoubleLinkedListNode:
def __init__(self, value, plus_node, dir=None):
self.value = value
self.minus_node = None
if plus_node:
plus_node.minus_node = self
if dir == None:
self.dir = plus_node.dir
if dir != None:
self.dir = dir
self.plus_node = plus_node
def __iter__(self):
self.cur = self
return self
def __next__(self):
if self.cur:
current = self.cur
if self.cur.dir:
if self.cur.dir[0] > 0:
self.cur = self.cur.plus_node
elif self.cur.dir[0] < 0:
self.cur = self.cur.minus_node
return current
else:
raise StopIteration
if __name__ == "__main__":
tail = DoubleLinkedListNode(5, None, [1])
head = DoubleLinkedListNode(1, DoubleLinkedListNode(2, DoubleLinkedListNode(3, DoubleLinkedListNode(4, tail))))
for i in head: print(i.value)
head.dir[0] = -1 # reverses direction for all nodes in the list
for i in tail: print(i.value)
class DoubleLinkedListNode:
def __init__(self, value, plus_node, dir=None):
self.value = value
self.minus_node = None
if plus_node:
plus_node.minus_node = self
if dir == None:
self.dir = plus_node.dir
if dir != None:
self.dir = dir
self.plus_node = plus_node
def __iter__(self):
self.cur = self
return self
def __next__(self):
if self.cur:
current = self.cur
if self.cur.dir:
if self.cur.dir[0] > 0:
self.cur = self.cur.plus_node
elif self.cur.dir[0] < 0:
self.cur = self.cur.minus_node
return current
else:
raise StopIteration
if __name__ == "__main__":
tail = DoubleLinkedListNode(5, None, [1]) # [1] sets direction for all nodes in list.
head = DoubleLinkedListNode(1, DoubleLinkedListNode(2, DoubleLinkedListNode(3, DoubleLinkedListNode(4, tail))))
for i in head: print(i.value) # Prints node values forwards from head.
head.dir[0] = -1 # Reverses direction for all nodes in the list.
for i in tail: print(i.value) # Prints node values in reverse from tail.
I have improved theYour code could be easily improved to handle these issues below. Preventing an endless loop requires keeping track of the identity of each node. I haveBelow, I've done this by incrementing a global variable node_count
, but there are more Pythonic ways to keep a count of instances of objects.
Below is an example of how \$\mathcal{0}(1)\$\$\mathcal{O}(1)\$ reversal of double-linked lists might be implemented using a direction flag. Points relevant to OP's code are:
A dedicated ́reverse_list ́
reverse_list
method is not really necessary.An ́iter ́
__iter__
method is more useful than a dedicated ́print_list ́print_list
method (Thanksthanks ChatterOne).Circular lists are also reversed neatly this way (note that ́iter ́
__iter__
cycles endlessly for circular lists).
I have improved the code to handle these issues below. Preventing an endless loop requires keeping track of the identity of each node. I have done this by incrementing a global variable node_count
, but there are more Pythonic ways to keep a count of instances of objects.
Below is an example of how \$\mathcal{0}(1)\$ reversal of double-linked lists might be implemented using a direction flag. Points relevant to OP's code are:
A dedicated ́reverse_list ́ method is not really necessary.
An ́iter ́ method is more useful than a dedicated ́print_list ́ method (Thanks ).
Circular lists are also reversed neatly this way (note that ́iter ́ cycles endlessly).
Your code could be easily improved to handle these issues. Preventing an endless loop requires keeping track of the identity of each node. Below, I've done this by incrementing a global variable node_count
, but there are more Pythonic ways to keep a count of instances of objects.
Below is an example of how \$\mathcal{O}(1)\$ reversal of double-linked lists might be implemented using a direction flag. Points relevant to OP's code are:
A dedicated
reverse_list
method is not really necessary.An
__iter__
method is more useful than a dedicatedprint_list
method (thanks ChatterOne).Circular lists are also reversed neatly this way (note that
__iter__
cycles endlessly for circular lists).