python链表的创建、插入和打印

class Node:
    def __init__(self, date=None, next=None):
        self.date = date        # 节点
        self.next = next

class Linkedlist:
    def __init__(self):
        self.head = None        # 表头

    def insert(self, date):
        newNode = Node(date)    # 创建插入数据的节点
        if self.head:
            current = self.head     # current相当于指针,在这指向头节点
            while current.next:
                current = current.next      # 如果目前节点的下一个节点不为空,则将指针等于下一个节点
            current.next = newNode      # 插入新节点newNode
        else:
            self.head = newNode     # 表示头节点指向为空,则头节点指向新节点

    def printLL(self):
        current = self.head
        flag = 0
        while current:
            if flag == 0:
                print(f"{current.date}", end="")
            else:
                print(f" {current.date}", end="")
            flag = 1
            current = current.next

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值