def main(): n1 = Node('say') n2 = Node('world') n1.next = n2 linked_list = Between_insert_linked_list(n1) linked_list.insert_between(n1, 'hello') linked_list.print_list()
def main(): n1 = Node('say') n2 = Node('hello') n1.next = n2 linked_list = Tail_Node_List(n1) linked_list.insert_tail('world') linked_list.print_list()
def insert_between(self, prev_node, data): if (prev_node == None): print('no previous node!') return new_node = Node(data) # 以下順序不能搞錯 new_node.next = prev_node.next # 新插入的node的next為原本prev_node的next prev_node.next = new_node # prev的next就是新的node
def insert_heading(self, newData): newNode = Node(newData) newNode.next = self.head # 把原本資料的頭接到新開頭的next, # e.g. 1 -> 2 -> 3 >> 新增一個0 >> 0 -> 1... self.head = newNode
from basic_node import Node from basic_node import printNodes class Node_List(): def __init__(self, headNode=None): self.head = headNode def print_list(self): printNodes(self.head) def insert_heading(self, newData): newNode = Node(newData) newNode.next = self.head # 把原本資料的頭接到新開頭的next, # e.g. 1 -> 2 -> 3 >> 新增一個0 >> 0 -> 1... self.head = newNode if (__name__ == '__main__'): n1 = Node(10) n2 = Node(20) n1.next = n2 linked_list = Node_List(n1) linked_list.insert_heading(30) linked_list.print_list()
from basic_node import Node from basic_node import printNodes n1 = Node('a') n2 = Node('b') n3 = Node('c') n1.next = n2 n2.next = n3 if (__name__ == '__main__'): # 只有此檔案才會被呼叫 printNodes(n1)