重點:html
鏈表(Linked list)是一種常見的基礎數據結構,是一種線性表,可是不像順序表同樣連續存儲數據,而是每個結點(數據存儲單元)裏存放下一個結點的信息(即地址)node
. is_empty():鏈表是否爲空python
. length():鏈表長度數據結構
. travel():遍歷整個鏈表app
. add(item):鏈表頭部添加元素spa
. append(item):鏈表尾部添加元素3d
. insert(pos, item):指定位置添加元素code
. remove(item):刪除節點htm
. search(item):查找節點是否存在對象
#封裝節點數據結構 class Node(): def __init__(self,item): self.item = item self.next = None def __str__(self): return self.item #封裝鏈表數據結構 class Link(): #初始化一個空鏈表 def __init__(self): #該屬性永遠指向第一個節點 self._head = None def isEmpty(self): return self._head == None def add(self,item): #建立一個新的節點對象 node = Node(item) #將節點插入到鏈表的頭部 node.next = self._head self._head = node def travel(self): cur = self._head while cur: print(cur.item) cur = cur.next def length(self): count = 0 cur = self._head while cur: count += 1 cur = cur.next return count def append(self,item): cur = self._head pre = None #cur前面節點的地址 node = Node(item) #若是鏈表爲空則新節點做爲鏈表中的第一個節點 if self._head is None: self._head = node return #鏈表非空對應的插入狀況 while cur: pre = cur cur = cur.next pre.next = node def insert(self,pos,item): cur = self._head pre = None node = Node(item) length = self.length() #對特殊狀況的處理 if pos > length: self.append(item) return if pos <= 0: self.add(item) return #正常處理 for i in range(pos): pre = cur cur = cur.next pre.next = node node.next = cur def remove(self,item): cur = self._head pre = None #若是刪除的是第一個節點 if item == cur.item: self._head = cur.next return while cur: if cur.item == item: pre.next = cur.next return else: pre = cur cur = cur.next def search(self,item): find = False cur = self._head while cur: if cur.item == item: find = True break cur = cur.next return find
link = Link() link.add(10) link.add('aa') link.append('bobo') link.append('bobo1') link.insert(111,666) # link.remove('aa') link.travel() print(link.length()) link.search(6669)
class Node(): def __init__(self,item): self.item = item self.left = None self.right = None class Tree(): def __init__(self): self.root = None def add(self,item): node = Node(item) if self.root is None: self.root = node return queue = [self.root] while queue: cur = queue.pop(0) if cur.left is None: cur.left = node return else: queue.append(cur.left) if cur.right is None: cur.right = node return else: queue.append(cur.right) def travel(self): if self.root is None: return queue = [self.root] while queue: cur = queue.pop(0) print(cur.item) if cur.left is not None: queue.append(cur.left) if cur.right is not None: queue.append(cur.right)
class Node(): def __init__(self,item): self.item = item self.left = None self.right = None class Tree(): def __init__(self): self.root = None def insertByOder(self,item): node = Node(item) if self.root is None: self.root = node return cur = self.root while True: if item < cur.item: if cur.left is None: cur.left = node return else: cur = cur.left else: if cur.right is None: cur.right = node return else: cur = cur.right def forward(self,root): if root is None: return # 根 左 右 print(root.item,end=' ') self.forward(root.left) self.forward(root.right) def mid(self,root): if root is None: return #左根右 self.mid(root.left) print(root.item,end=' ') self.mid(root.right) def back(self,root): if root is None: return #左右根 self.back(root.left) self.back(root.right) print(root.item,end=' ')