Tuesday, 3 December 2013

linked list

On Oct 22-24
we learned linked list in class.
linked list is about a sequence of nodes, each with a value and reference to next node. list has reference to front node.
and there are two kinds of operations insert a node and find a node.
besides, we can also implement stack using linked in list:
 def __init__(self: stack)
   self.top=None
 def pop
 def is_empty
 def push

the linked in list class can be created as follows:

class LinkedList:
    def __init__(self):
        self.length = 0
        self.head   = None
One nice thing about the LinkedList class is that it provides a natural place to put wrapper functions like print_backward_nicely, which we can make a method of the LinkedList class:
class LinkedList:
    ...
    def print_backward(self):
        print "[",
        if self.head != None:
            self.head.print_backward()
        print "]",

class Node:
    ...
    def print_backward(self):
    if self.next != None:
        tail = self.next
        tail.print_backward()
    print self.cargo, 
 
i found it very difficult to understand the linked in list in the first place. it befuddled me about 
how to make a reference to the next value. it should be like every item contain a value and also a reference
that can lead us to the next item, and the reference in the next value also take us to the other item.
 
 

No comments:

Post a Comment