szaydel
7/30/2013 - 4:42 AM

A very basic linked list in python.

A very basic linked list in python.

class Node:
    def __init__(self, cargo=None, next=None):
        self.cargo = cargo
        self.next  = next
    def __str__(self):
        return str(self.cargo)

# => Create a list of Node objects, each with .next being None
linked = [Node(x) for x in range(0,100)]

# => Link each item in the list to next item up to `len(linked) - 1`
for idx, value in enumerate(linked): 
    if idx < len(linked) - 1:
        linked[idx].next = linked[idx+1]

# => Linking nodes in the list 
node = linked[0]
while node.next is not None:
    print node.cargo
    node = node.next