python实现单向循环链表 判空 长度 遍历 头插 尾插 插入 删除 范围查询 反转



class Node(object):
    """定义节点"""

    def __init__(self, obj):
        self.data = obj
        self.next = None


class CycleLinkList(object):
    """单向循环链表"""

    def __init__(self):
        self._head = None

    def is_empty(self):
        """是否为空"""
        return self._head is None

    def length(self):
        """单向循环链表的 长度"""
        cur = self._head
        if cur is None:
            return 0

        count = 1
        while cur.next != self._head:
            count += 1
            cur = cur.next

        return count

    def travel(self):
        """单向循环链表 遍历"""
        cur = self._head
        if cur is None:
            print()

        while cur.next != self._head:
            print(cur.data, end=" ")
            cur = cur.next
        # 打印尾节点
        print(cur.data)

    def add(self, obj):
        """单向循环链表 头插法"""
        node = Node(obj)

        cur = self._head
        if cur is None:
            node.next = node

        while cur.next != self._head:
            cur = cur.next

        node.next = self._head
        cur.next = node
        self._head = node

    def append(self, obj):
        """单向循环链表 尾插法"""
        node = Node(obj)

        cur = self._head
        if cur is None:
            node.next = node

        while cur.next != self._head:
            cur = cur.next

        cur.next = node
        node.next = self._head

    def insert(self, index, obj):
        """单向循环链表 插入"""

        assert isinstance(index, int)
        if index <= 0:
            self.add(obj)
        elif index >=self.length():
            self.append(obj)
        else:
            # 说明是在中间插入
            node = Node(obj)
            cur = self._head
            for i in range(index - 1):
                cur = cur.next
            node.next = cur.next
            cur.next = node

    def remove(self, obj):
        """单向循环链表 移除"""
        cur = self._head

        if cur is None:
            raise ValueError("%s does not exist" % obj)
        # 如果是单个节点
        if cur.next == self._head:
            if cur.data == obj:
                self._head = None
            else:
                raise ValueError("%s does not exist" % obj)

        # 难点: 删除的是头结点
        if cur.data == obj:
            # 遍历找到尾节点
            while cur.next != self._head:
                cur = cur.next
            cur = self._head.next
            self._head = self._head.next
            # 对于c程序老说, 这里还有做一个内存回收

        while cur.next != self._head:
            pre = cur
            cur = cur.next
                
            if cur.data == obj:
                pre.next = cur.next
                break
        else:
            raise ValueError("%s does not exist" % obj)
    
    def search(self, obj):
        """单向循环链表 范围查询"""
        cur = self._head
        
        if cur is None:
            return False
        
        while cur.next != self._head:
            if cur.data == obj:
                return True
            cur = cur.next
        else:
            if cur.data == obj:
                return True
            else:
                return False
    
    def reverse(self):
        """单向循环链表 反转"""
        # TODO 此坑待填


if __name__ == '__main__':
    cll = CycleLinkList()
    print(cll.is_empty())  # True
    print(cll.length())  # 0

    cll.add(100)
    print(cll.is_empty())  # False
    print(cll.length())  # 1
    cll.travel()  # 100

    cll.add(200)
    print(cll.length())  # 2
    cll.travel()  # 200 100

    cll.add(300)
    cll.travel()  # 300 200 100

    cll.append(400)
    cll.travel()  # 300 200 100 400

    cll.insert(0, 500)
    cll.travel()  # 500 300 200 100 400

    cll.insert(10, 600)
    cll.travel()  # 500 300 200 100 400 600

    cll.insert(2, 700)
    cll.travel()  # 500 300 700 200 100 400 600

    cll.remove(500)
    cll.travel()  # 300 700 200 100 400 600

    cll.remove(600)
    cll.travel()  # 300 700 200 100 400

    cll.remove(200)
    cll.travel()  # 300 700 100 400

    print(cll.search(100))  # True
    print(cll.search(800))  # False




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值