栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 单链表学习

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 单链表学习

python学习日记:

现在在地基系统学习python,记录一下自己每天学习python的日常,今天在刷题的时候看到一道题是要用链表做的,自己忘的差不多了,重新学习一下,希望也对大家有所帮助,每天学习内容太多了,没时间仔细写注解,直接放代码和结果了
`

学习内容:

python 单链表的基本操作

例如:

  1. 链表创建
  2. 节点的增删查改
  3. 链表反转

代码块:
class ListNode:
    def __init__(self, val):
        self.val = val  # 值
        self.next = None


class LinkedList:
    def __init__(self):
        self.head = None  # 头节点
        self.tail = None  # 尾节点

    def isEmpty(self):  # 判断链表是否为空
        return self.head is None  # 空链表返回1

    def size(self):  # 统计链表节点个数
        if self.isEmpty():
            print("链表为空")
            return
        cur = self.head
        count = 0
        while cur:
            count += 1
            cur = cur.next
        return count

    def append(self, val):  # 尾插 参数:插入的单个值
        node = ListNode(val)
        if self.head is None:
            self.head = node
            self.tail = node
        else:
            self.tail.next = node
            self.tail = node

    def appendList(self, list):  # 尾插 参数:插入的列表
        for i in list[0:]:
            node = ListNode(i)
            if self.head is None:
                self.head = node
                self.tail = node
            else:
                self.tail.next = node
                self.tail = node

    def printAll(self):  # 输出所有元素
        if self.isEmpty():
            print("链表为空")
            return
        cur = self.head
        while cur:
            print(cur.val, end='t')
            cur = cur.next
        print()

    def printVal(self):  # 调用next()逐个输出 g = list.printVal() print(next(g))
        if self.isEmpty():
            print("链表为空")
            return
        cur = self.head
        while cur:
            yield cur.val
            cur = cur.next
        print("已到尾节点")
        return

    def insert(self, index, value):  # 插入节点 头结点下标为0 参数:插入节点下标(插入后的链表节点下标) 插入值
        if self.isEmpty():
            print("链表为空")
            return
        if index > self.size():
            print(f'边界值为:{self.size()},已超过边界值')
            return
        cur = self.head
        if index == 0:  # 插入的节点为头节点时,更新头节点
            node = ListNode(value)
            node.next = self.head
            self.head = node
            return
        while index - 1:
            cur = cur.next
            index -= 1
        node = ListNode(value)
        if not cur.next:  # 插入节点为尾节点时,更新尾节点
            self.tail = node
        node.next = cur.next
        cur.next = node

    def update(self, index, value):  # 修改某一节点值
        if self.isEmpty():
            print("链表为空")
            return
        if index >= self.size():
            print(f'边界值为:{self.size()},已超过边界值')
            return
        cur = self.head
        while index:
            cur = cur.next
            index -= 1
        cur.val = value
        print("修改成功")

    def delete(self, index):  # 删除节点 头节点为0 参数:节点下标(当前链表节点下标)
        if self.isEmpty():
            print("链表为空")
            return
        if index >= self.size():
            print(f'边界值为:{self.size()},已超过边界值')
            return
        if self.head is self.tail:  # 删除只有一个节点的链表
            self.head = None
            self.tail = None
            return
        if index == 0:  # 删除头节点时,头节点重新赋值
            self.head = self.head.next
            return
        cur = self.head
        while index - 1:
            cur = cur.next
            index -= 1
        cur.next = cur.next.next  # 删除对应节点
        if not cur.next:  # 删除尾节点时,尾节点重新赋值
            self.tail = cur

    def reverse(self):  # 链表反转
        newHead = None
        cur = self.head
        while cur:
            cur2 = ListNode(cur.val)  # 头插
            cur2.next = newHead
            newHead = cur2
            cur = cur.next
        self.head = newHead
        print("反转成功")
        return

    def search(self, value):#查询某个值
        cur = self.head
        index = 0
        while cur:
            if cur.val == value:
                print(f"数值{value}在第{index}个节点")
                return
            index += 1
            cur = cur.next
        print("数值不存在")


list = LinkedList()
print("==插入单个数值==")
list.append(0)
list.printAll()
print("==插入链表==")
list.appendList([1, 2, 3, 4])
list.printAll()
print("==统计节点数==")
print(list.size())
print("==调用next逐个输出==")
func = list.printVal()
print(next(func))
print(next(func))
print("==插入节点==")
list.insert(5, 5)
list.printAll()
print("==删除节点==")
list.delete(5)
list.printAll()
print("==修改节点==")
list.update(4, 8)
list.printAll()
print("==反转链表==")
list.reverse()
list.printAll()

---

# 运行结果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/67c7af31ffa74b65aaf2b02ec3eeebe1.png#pic_center)

![在这里插入图片描述](https://img-blog.csdnimg.cn/c10bea682386498ab6d01d7e53bbe3e4.png#pic_center)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/987221.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号