python链表的概念和用法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

python链表的概念和用法

一叶知秋的BLOG   2022-06-03 我要评论

本文为大家分享了python链表的基础概念和基础用法,供大家参考,具体内容如下

一、什么是链表

链表是由多个不同的节点组成,每个节点通过指针区域关联到一起
链表的头指针,指向了头节点,通过头指针可以找到其他节点信息

二、什么是节点

链表由节点组成,每个节点又包含两个部分,一个是元素区域,一个是指针区域。
元素区域存储的是,当前节点的数值,指针区域指向下一个节点的对象。在C语言中,指针应该是指向下一个元素的的内存地址,因python中不研究指针,这里用下一个节点的对象代替。这样我们就能通过指针区域,找到下一个节点的信息,从而得到下一个节点的值了。

三、为什么引入链表的概念

1.先说说数组这种数据结构吧,数组是一块大的连续内存空间。每次初始化需要开辟一大块内存空间,空间利用率比较低。而链表则不同,链表的节点可以随机分布在任何位置,只需通过指针穿引起来即可。
2.在连续的内存空间中,插入一个元素时,所有其他元素的位置也变动了。插入元素、删除元素时候,效率比较低。
链表是非连续的内存空间,每个节点单独存在自己的内存空间,通过指针指向下一个节点。
如果在某个地方插入一个节点,只需要改变指针的指向即可,不用其他元素都变动。

四、链表的基本操作

# 实现头部插入 尾部插入 根据索引插入 删除节点并print 打印
# 定义一个节点
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None


class SingleLinkList:

    def __init__(self):
        self.head = None
        self.tail = None

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

    def length(self):
        """求当前链表的长度
        :return:
        """
        count = 0
        cur = self.head

        while cur is not None:
            count += 1
            cur = cur.next

        return count

    def insert_head_node(self, data):
        """链表头部添加元素
        :param data: 要保存的数据
        :return:
        """
        node = Node(data)
        node.next = self.head
        self.head = node

    def append_node(self, data):
        """链表尾部添加元素,有多种实现方式
        :param data:
        :return:
        """
        # 第一种方式  时间复杂度为O(n)的处理方式
        node = Node(data)
        # 如果链表为空,需要特殊处理
        if self.is_empty():
            self.head = node
        else:
            cur = self.head
            while cur.next is not None:
                cur = cur.next
            # 退出循环时, cur指向尾节点
            cur.next = node

        # 第二种 引入一个tail指针 默认当前链表为一个空链表,不停的去append节点

        # node = Node(data)
        # if self.is_empty():  # 当第一次添加节点到空链表中的时候,头指针和尾指针同时指向新节点
        #     self.head = node
        #     self.tail = node

        # else:
        # 当再次添加节点到链表中的时候, 头指针始终指向头节点,尾指针始终执行未节点,如果一直向未节点追加节点,只需移动tail指针即可
        #     self.tail.next = node
        #     self.tail = node

    def insert_node(self, pos, data):
        """指定位置添加元素
        :param pos:
        :param data:
        :return:
        """
        # 1, 在头部添加
        if pos <= 0:
            self.insert_head_node(data)
        # 2, 在尾部添加
        elif self.length() >= pos:
            self.append_node(data)
        # 3, 指定位置添加
        else:
            count = 0
            while count < (pos - 2):
                count += 1
                self.head = self.head.next

            # 这时候self.head 表示当前插入前一个节点
            # self.head.next 表示当前插入的后一个节点
            node = Node(data)
            self.head.next = node
            node.next = self.head.next

    def delete_node(self, data):
        """删除节点
        :param data:
        :return:
        """
        cur = self.head     # 记录当前节点的位置
        pre = None          # 记录当前节点位置的前置节点
        while cur is not None:
            # 找到了要删除的元素
            if cur.data == data:
                # 在头部找到了要删除的元素
                if cur == self.head:
                    self.head = cur.next
                    return True
                else:
                    pre.next = cur.next
                    return True
            else:
                # 不是要找的元素, 移动光标
                pre = cur
                cur = cur.next

    def search_node(self, data):
        """查找节点是否存在
        :return:
        """
        cur = self.head
        while cur is not None:
            if cur.data == data:
                return True
            cur = cur.next
        return False

    def reveres_node(self):
        """链表反转
        :return:
        """
        if self.is_empty():
            return
        j = 0
        while j < self.length() - 1:
            cur = self.head
            for i in range(self.length() - 1):
                cur = cur.next
                if cur.next is None:
                    x = cur.data
                    self.delete_node(cur.data)
                    self.insert_node(j, x)
            j += 1
        return self.head

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们