2015年3月9日星期一

Link List

    Link list likes tree what we learn before.  However there are quite different between them.  Link list only have one node of one root which is unlike tree, having 2 or 3 nodes.
    Firstly, we need a class, which likes a tree class, to record a value that before of after a value.
    Secondly, we can create a link list class.  In the __init__ function, we create two variable to record a link list in the following method.
    
    Following, it is the method that use to append a value before a value.
    Lastly, it is the method that use to append a value after a value.

def append(lnk, value):
        ''' (LinkedList, object) -> NoneType

        Insert a new node with value at back of lnk.

        >>> lnk = LinkedList()
        >>> lnk.append(5)
        >>> lnk.size
        1
        >>> print(lnk.front)
        5 ->|
        >>> lnk.append(6)
        >>> lnk.size
        2
        >>> print(lnk.front)
        5 -> 6 ->|
        '''
        new_node = LLNode(value)
        if lnk.back:
            lnk.back.nxt = new_node
            lnk.back = new_node
        else:
            lnk.back = lnk.front = new_node
        lnk.size += 1

没有评论:

发表评论