class Node: def __init__(self,value): self.value = value self.next = None class LinkedList: def __init__(self): self.head = None def add_front(self,value): new_node = Node(value) new_node.next = self.head self.head = new_node def add_back(self,value): new_node = Node(value) 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 print_list(self): current = self.head while current is not None: print(current.value) current = current.next list1 = LinkedList() list1.add_front(1) list1.add_front(2) list1.add_back(3) list1.print_list()
1。节点类:
2.链表类:
3. add_front方法:
4。 add_back方法:
5。 print_list 方法:
6。用法示例:
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3