class BlockNode: # 定义块节点类 def __init__(self, data): # 构造函数,初始化节点数据 self.data = data self.next = None class BlockLinkedList: # 定义块链表类 def __init__(self): # 构造函数 self.head = None # 初始化头节点为 None def insert(self, data): # 插入数据的方法 new_node = BlockNode(data) # 创建新节点 if self.head is None: # 如果头节点为空 self.head = new_node # 则将新节点设置为头节点 else: current = self.head # 否则,从头节点开始 while current.next is not None: # 循环直到找到最后一个节点 current = current.next current.next = new_node # 将新节点添加到最后 def display(self): # 显示链表数据的方法 current = self.head # 从头节点开始 while current: # 循环直到节点为空 print(current.data) # 打印当前节点的数据 current = current.next # 移动到下一个节点 block_list = BlockLinkedList() # 创建块链表对象 block_list.insert(['A', 'B', 'C', 'D']) # 插入数据 block_list.insert(['E', 'F', 'G', 'H']) # 插入数据 block_list.insert(['张三', 'F', 'G', 'H']) # 插入数据 block_list.display() # 显示链表数据
python数据结构——动态链式存储插入
于 2024-04-24 10:56:13 首次发布