3-3 single circular list

One-way circular list

A singly linked list is modified cycle chain way, next field of the last node in the list is not None, but point to the head of the linked list of nodes.

 

achieve

class the Node (Object):
     "" " node " "" 
    DEF  the __init__ (Self, Item): 
        self.item = Item 
        self.next = None 


class SinCycLinkedlist (Object):
     "" " unidirectional circular list " "" 
    DEF  the __init__ ( Self): 
        self._head = None 

    DEF is_empty (Self):
         "" " determines whether the list is empty ", "" 
        return self._head == None 

    DEF length (Self):
         "" " the resulting list length " "" 
        # If the list is empty, return length 0 
        IF self.is_empty():
            return 0
        count = 1
        cur = self._head
        while cur.next != self._head:
            count += 1
            cur = cur.next
        return count

    def travel(self):
        """遍历链表"""
        if self.is_empty():
            return
        cur = self._head
        print cur.item,
        while cur.next != self._head:
            cur =cur.next
             Print cur.item,
         Print  "" 


    DEF the Add (Self, Item):
         "" " head Add Node " "" 
        Node = the Node (Item)
         IF self.is_empty (): 
            self._head = Node 
            node.next = self._head
         the else :
             # node to add points to _head 
            node.next = self._head
             # moved to list the tail, the tail node's next point to the node 
            CUR = self._head
             the while cur.next =! self._head:
                CUR = cur.next 
            cur.next = node
             # _head point add node ' 
            self._head = node 

    DEF the append (Self, Item):
         "" " Add Node tail " "" 
        node = the Node (Item)
         IF self.is_empty () : 
            self._head = the Node 
            node.next = self._head
         the else :
             # move the tail of the list 
            CUR = self._head
             the while cur.next =! self._head: 
                CUR =cur.next
             # The node pointing to the tail node 
            cur.next = node
             # will point to the head node node _head 
            node.next = self._head 

    DEF INSERT (Self, POS, Item):
         "" " add nodes in the specified location " "" 
        IF POS <= 0: 
            self.add (Item) 
        elif POS> (self.length () -. 1 ): 
            self.append (Item) 
        the else : 
            Node = the Node (Item) 
            CUR = self._head 
            COUNT = 0
             # moved to before a position designated location
             the while COUNT <(-POS. 1 ): 
                COUNT + =. 1 
                CUR = cur.next 
            node.next = cur.next 
            cur.next = Node 

    DEF Remove (Self, Item):
         "" " Delete a node "" " 
        # If the list is empty, then direct return 
        IF self.is_empty ():
             return 
        # will point to the head node cur 
        cur = self._head 
        pre = None
         # If the head node element is the element you want to find Item 
        IF cur == .Item Item:
            # If you list more than one node 
            IF cur.next =! Self._head:
                 # first find the end node, the next node to the end of the second node 
                the while cur.next =! Self._head: 
                    CUR = cur.next
                 # CUR point the tail node 
                cur.next = self._head.next 
                self._head = self._head.next
             the else :
                 # list only one node 
                self._head = None
         the else : 
            pre = self._head
             # the first node is not to be removed 
            while= cur.next! self._head:
                 # found the element to be deleted 
                if cur.item == Item:
                     # delete 
                    pre.next = cur.next
                     return 
                the else : 
                    pre = CUR 
                    CUR = cur.next
             # CUR point to the end node 
            if == cur.item Item:
                 # tail delete 
                pre.next = cur.next 

    DEF Search (Self, Item):
         "" " to find whether there is a node " "" 
        iF self.is_empty ():
            return False
        cur = self._head
        if cur.item == item:
            return True
        while cur.next != self._head:
            cur = cur.next
            if cur.item == item:
                return True
        return False

if __name__ == "__main__":
    ll = SinCycLinkedlist()
    ll.add(1)
    ll.add(2)
    ll.append(3)
    ll.insert(2, 4)
    ll.insert(4, 5)
    ll.insert(0, 6)
    print "length:",ll.length()
    ll.travel()
    print ll.search(3)
    print ll.search(7)
    ll.remove(1)
    print "length:",ll.length()
    ll.travel()

 

Guess you like

Origin www.cnblogs.com/echo-kid-coding/p/11318465.html