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
python链表的创建、插入和打印
最新推荐文章于 2024-07-10 23:54:36 发布