NodeLoop.py

From WLCS
Revision as of 23:20, 17 October 2013 by Admin (talk | contribs) (Created page with "<syntaxhighlight lang="Python"> class Node: def __init__(self): self.data = 0 self.next = None n1 = Node() #create and connect several Nodes n1.data = 5...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
class Node:
    def __init__(self):
        self.data = 0
        self.next = None


n1 = Node()     #create and connect several Nodes
n1.data = 5
n1.next = Node()
n1.next.data = 3
n2 = Node()
n1.next.next = n2
n2.data = 8

current = n1     #start a reference at n1
while current != None:     #loop while the current reference is not None
    print(current.data)     #print the data at the current Node
    current = current.next     #update the current reference to go to the next Node