单向循环链表 Python实现

2019-07-30  本文已影响0人  Yuanshuo
The core values of Chinese socialism

单向循环链表

class SingleNode(object):
    def __init__(self, item):
        self.item = item
        self.next = None

class SingleCycLinkedlist(object):
    def __init__(self):
        self._head = None

    def isEmpty(self):
        return self._head == None

    def length(self):
        if self.isEmpty():
            return 0
        count = 1
        cur = self._head
        while cur.next != self._head:
            count += 1
            cur = cur.next
        return count

    def travel(self):
        if self.isEmpty():
            return
        cur = self._head
        print(cur.item)
        while cur.next != self._head:
            cur = cur.next
            print(cur.item)

    def addFirst(self, item):
        node = SingleNode(item)
        if self.isEmpty():
            self._head = node
            node.next = self._head
        else:
            node.next = self._head
            cur = self._head
            while cur.next != self._head:
                cur = cur.next
            cur.next = node
            self._head = node

    def append(self, item):
        node = SingleNode(item)
        if self.isEmpty():
            self._head = node
            node.next = self._head
        else:
            cur = self._head
            while cur.next != self._head:
                cur = cur.next
            cur.next = node
            node.next = self._head

    def insert(self, pos, item):
        if pos <= 0:
            self.addFirst(item)
        elif pos > (self.length()-1):
            self.append(item)
        else:
            node = SingleNode(item)
            cur = self._head
            count = 0
            while count < (pos-1):
                count += 1
                cur = cur.next
            node.next = cur.next
            cur.next = node

    def remove(self, item):
        if self.isEmpty():
            return
        cur = self._head
        pre = None
        if cur.item == item:
            if cur.next != self._head:
                while cur.next != self._head:
                    cur = cur.next
                cur.next = self._head.next
                self._head = self._head.next
            else:
                self._head = None
        else:
            pre = self._head
            while cur.next != self._head:
                if cur.item == item:
                    pre.next = cur.next
                    return
                else:
                    pre = cur
                    cur = cur.next
            if cur.item == item:
                pre.next = cur.next

    def search(self, item):
        if self.isEmpty():
            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 = SingleCycLinkedlist()
    ll.addFirst(1)
    ll.addFirst(2)
    ll.append(3)
    ll.insert(2, 4)
    ll.insert(4, 5)
    ll.insert(0, 6)
    print("length: {0}".format(ll.length()))
    ll.travel()
    print(ll.search(3))
    print(ll.search(7))
    ll.remove(1)
    print("length:",ll.length())
    ll.travel()
length: 6
6
2
1
4
3
5
True
False
length: 5
6
2
4
3
5
上一篇下一篇

猜你喜欢

热点阅读