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