Single Circular Link List in Python

Single Circular Link List

class Node(object):
    '''this is a node'''
    def __init__(self, elem):
        self.elem = elem
        self.next = None

class SingleCircularLinkList(object):
    '''this is a Singly Linked List'''
    def __init__(self, node=None):
        self.__head = node
        if node:
            node.next = node

    def is_empty(self):
        '''Judge whether it is empty'''
        return self.__head == None

    def length(self):
        '''Length of linked list'''
        # cursor
        cur = self.__head
        if self.is_empty():
            return 0
        # count
        count = 1
        while cur.next != self.__head:
            count += 1
            cur = cur.next
        return count

    def travel(self):
        '''Traversing chain list'''
        if self.is_empty():
            return
        cur = self.__head
        while cur.next != self.__head:
            print(cur.elem, end=' ')
            cur = cur.next
        print(cur.elem)

    def add(self, item):
        '''Add elements to linked list header'''
        node = Node(item)
        if self.is_empty():
            self.__head = node
            node.next = node
        else:
            cur = self.__head
            while cur.next != self.__head:
                cur = cur.next
            node.next = self.__head
            self.__head = node
            cur.next = self.__head

    def append(self, item):
        '''Add elements to the end of the linked list'''
        node = Node(item)
        if self.is_empty():
            self.__head = node
            node.next = node
        else:
            cur = self.__head
            while cur.next != self.__head:
                cur = cur.next
            node.next = self.__head
            cur.next = node

    def insert(self, pos, item):
        '''Add elements to specified locations'''
        if pos <= 0:
            self.add(item)
        elif pos > self.length():
            self.append(item)
        else:
            node = Node(item)
            pre = self.__head
            count = 0
            while count < pos:
                count += 1
                pre = pre.next
            node.next = pre.next
            pre.next = node

    def remove(self, item):
        '''Delete node'''
        if self.is_empty():
            return
        cur = self.__head
        pre = None
        while cur.next != self.__head:
            if cur.elem == item:
                if cur == self.__head:
                    rear = self.__head
                    while rear.next != self.__head:
                        rear = rear.next
                    self.__head = cur.next
                    rear.next = self.__head
                else:
                    pre.next = cur.next
                return
            else:
                pre = cur
                cur = cur.next
        if cur.elem == item:
            if self.length() > 1:
                pre.next = cur.next
            else:
                self.__head = None

    def search(self, item):
        '''Judge node existence'''
        if self.is_empty():
            return False
        cur = self.__head
        while cur.next != self.__head:
            if cur.elem == item:
                return True
            else:
                cur = cur.next
        if cur.elem == item:
            return True
        return False

def main():
    s1 = SingleCircularLinkList()
    s1.append(1)
    print(s1.length())
    print(s1.is_empty())
    s1.append(2)
    s1.travel()
    s1.insert(1,3)
    s1.travel()
    s1.remove(3)
    s1.travel()
    print(s1.search(2))

if __name__ == '__main__':
    main()


---------------results of enforcement----------
1
False
1 2
1 2 3
1 2
True

-----------------------------------------------

猜你喜欢

转载自blog.csdn.net/Xcq007/article/details/82048629