• 技术文章 >Python技术 >Python基础教程

    python创建链表的两种形式

    小妮浅浅小妮浅浅2021-09-26 09:34:31原创3245

    说明

    1、头插法将结点插入头结点后面,新加入的结点next指向原来head指向的结点。

    head改为新的结点。

    2、尾插法将结点插入尾点前,新节点的next指向tail,tail更新为新节点。

    实例

    class Node:
        def __init__(self,item):
            self.item = item
            self.next = None
     
    class HandleNode:
        def create_linklist_head(self,li):
            head = Node(li[0])
            for element in li[1:]:
                node = Node(element)
                node.next = head
                head = node
            return head
     
        def create_linklist_tail(self,li):
            head = Node(li[0])
            tail = head
            for element in li[1:]:
                node = Node(element)
                tail.next = node
                tail = node
            return head
     
        def print_linklist(self,head):
            while head:
                print(head.item,end=',')
                head=head.next

    以上就是python创建链表的两种形式,希望对大家有所帮助。更多Python学习指路:python基础教程

    本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

    专题推荐:python 链表
    品易云
    上一篇:python中mypy是什么 下一篇:python面向过程的优缺点

    相关文章推荐

    • python如何实现图像等比缩放• python setup和teardown的使用• python绘制散点图的两种方法• python用plt.pie绘制饼图• python Axes3D绘制3D图形• python raise触发异常的实现• python链表法的优缺点• python有序Dict的原理• python探针如何实现• python如何制作探针模块• python poetry如何创建项目• python poetry创建虚拟环境• python poetry如何安装依赖• python中flake8是什么• python中mypy是什么

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网