如何用Python实现单链表?

18 篇文章 1 订阅
2 篇文章 0 订阅

节点抽象类型定义

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

链表抽象类型

class SingleList:
    def __init__(self, head_node=None):
        self.__head = head_node

    def is_empty(self):  # 判断链表是否为空
        if self.__head is None:
            return True
        else:
            return False

    def length(self):  # 返回链表长度
        # cursor:游标,用来移动遍历节点
        cursor = self.__head
        count = 0  # 记录节点数量
        while cursor is not None:
            count += 1
            cursor = cursor.next
        return count

    def travel(self):  # 遍历链表
        # cursor:游标,用来移动遍历节点
        cursor = self.__head
        while cursor is not None:
            print(cursor.item, end=' ')
            cursor = cursor.next
        print()

    def add(self, item):  # 在链表头添加元素
        new_node = SingleNode(item)
        new_node.next = self.__head
        self.__head = new_node

    def append(self, item):  # 在链表尾添加元素
        new_node = SingleNode(item)
        cursor = self.__head
        if self.is_empty():
            self.__head = new_node
        else:
            while cursor.next:
                cursor = cursor.next
            cursor.next = new_node

    def insert(self, pos, item):  # 在指定位置添加元素
        new_node = SingleNode(item)
        cursor = self.__head
        if pos < 0 or not isinstance(pos, int):
            print('节点位置必须是正整数!')
        elif self.length() + 1 < pos:
            print('插入位置已超过链表最大长度!')
        else:
            if pos == 1:
                new_node.next = self.__head
                self.__head = new_node
            else:
                for _ in range(1, pos - 1):
                    cursor = cursor.next
                new_node.next = cursor.next
                cursor.next = new_node

    def remove(self, item, whole=False):  # 删除节点
        cursor = self.__head
        if whole:
            while cursor:
                if cursor.item == item:
                    self.__head = cursor.next
                    cursor = self.__head
                elif cursor.next and cursor.next.item == item:
                    cursor.next = cursor.next.next
                else:
                    cursor = cursor.next
        else:
            while cursor:
                if cursor.item == item:
                    self.__head = cursor.next
                    break
                elif cursor.next and cursor.next.item == item:
                    cursor.next = cursor.next.next
                    break
                cursor = cursor.next

    def search(self, item):  # 查找节点是否存在
        cursor = self.__head
        while cursor:
            if cursor.item == item:
                return True
            else:
                cursor = cursor.next
        return False

    def count(self, item):  # 统计item的数量
        count = 0
        cursor = self.__head
        while cursor:
            if cursor.item == item:
                count += 1
            cursor = cursor.next
        return count

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值