python-实现单向循环链表

# encoding=utf-8


class Node(object):
    """节点类"""
    def __init__(self, item):
        """
        初始化函数
        :param item:  要保存的用户数据
        """
        self.item = item
        self.next = None


class CycleSingleLinkList(object):
    """循环单链表"""
    def __init__(self, node=None):
        """
        初始化函数r
        :param node: 可传可不传的头节点
        """
        # 指向头节点
        self.__head = node

    def is_empty(self):
        """链表是否为空"""
        return self.__head is None

    def length(self):
        """链表长度"""
        if self.is_empty():
            return 0

        current = self.__head
        count = 1

        while current.next != self.__head:
            count += 1
            current = current.next
        return count

    def travel(self):
        """遍历整个链表 要考虑空链表如果是空链表判断条件不成立也就不输出任何信息"""
        if self.is_empty():
            print("")
            return
        current = self.__head
        while current.next != self.__head:
            print(current.item, end=" ")
            current = current.next

        # 退出循环时游标指向了尾节点
        print(current.item, end=" ")
        print("")  # 多输出换行

    def add(self, item):
        """"在链表头部插入元素"""
        node = Node(item)
        if self.is_empty():
            node.next = node
            self.__head = node
        else:
            cur = self.__head
            # 寻找尾节点
            while cur.next != self.__head:
                cur = cur.next
            node.next = self.__head
            self.__head = node
            cur.next = node

    def append(self, item):
        """"在链表尾部插入节点"""
        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):
        """指定位置添加元素"""
        # 在头结点插入元素
        if pos <=0:
            self.add(item)
        # 在链表的尾部添加元素
        elif pos >= self.length():
            self.append(item)

        # 在链表的任意位置添加元素

        else:
            cur = self.__head
            count = 0
            # 找到添加位置的前一个位置
            while count < (pos-1):
                count += 1
                cur = cur.next
            node = Node(item)
            node.next = cur.next
            cur.next = node

    def remove(self, item):
        """
        删除节点
        :param item: 要删除的元素
        :return:
        """
        if self.is_empty():
            return
        cur = self.__head
        pre = None
        while cur.next != self.__head:
            # 找到了元素
            if cur.item == item:
                # 在头部找到了元素
                if cur == self.__head:
                    rel = self.__head
                    while rel.next != self.__head:
                        rel = rel.next
                    self.__head = cur.next
                    rel.next = self.__head
                else:
                    pre.next = cur.next
                return
            pre = cur
            cur = cur.next
        # 退出循环后, cur指向了尾节点
        if cur.item == item:
            if pre:
                pre.next = self.__head
            else:
                # 链表只有一个节点
                self.__head = None

    def search(self, item):
        """
        查找节点是否存在
        :param item: 要查找的element
        :return:
        """
        cur = self.__head

        while cur.next != self.__head:
            if cur.item == item:
                return True
            cur = cur.next
        # 退出循环后, cur指向了尾节点
        if cur.item == item:
            return True
        return False

if __name__ == '__main__':
    ll = CycleSingleLinkList()
    print(ll.length())

    ll.append(1)   # 1
    print(ll.length())
    ll.travel()

    ll.append(2)   #1 2
    print(ll.length())
    ll.travel()

    ll.add(3)   # 3 1 2
    ll.travel()

    ll.add(4) # 4 3 1 2
    ll.travel()

    ll.insert(0, 5)  # 5 4 3 1 2
    ll.travel()

    ll.insert(10, 6)  # 5 4 3 1 2  6
    ll.travel()

    ll.insert(3, 7)  # 5 4 3  7 1 2  6
    ll.travel()

    ll.remove(5)  #  4 3  7 1 2  6)
    ll.travel()

    ll.remove(6)  #  4 3  7 1 2
    ll.travel()

    ll.remove(7)  #  4 3  1 2
    ll.travel()

    ll.remove(4)  #  3  1 2
    ll.travel()
    ll.remove(3)  #   1 2
    ll.travel()
    ll.remove(1)  #  2
    ll.travel()
    ll.remove(2)
    ll.travel()

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/wgDream/p/7525966.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞