Singly-linked list/Element definition: Difference between revisions

Content added Content deleted
(Logo)
Line 244: Line 244:
The Node class implements also iteration for more Pythonic iteration over linked lists.
The Node class implements also iteration for more Pythonic iteration over linked lists.


<python>class Node:
<pre>
class Node:
def __init__(self, value, next=None):
def __init__(self, value, next=None):
self.value = value
self.value = value
Line 253: Line 252:
while node:
while node:
yield node
yield node
node = node.next
node = node.next</python>
</pre>
(Note that in Python you would always use the built-in list structure for this task, except for the specific case, such as this, when you are asked to show how you might implement the function without using the list type).
(Note that in Python you would always use the built-in list structure for this task, except for the specific case, such as this, when you are asked to show how you might implement the function without using the list type).