圖示說明:
單向鏈表:
insert、 remove、 update、pop方法
class Node:def __init__(self, data):self.data = dataself.next = Nonedef __str__(self):return str(self.data)# 通過單鏈表構建一個list的結構: 添加 刪除 插入 查找 獲取長度 判斷是否為空...
# list1 = [] list1.append(5) [5,] slist = SingleList() slist.append(5)
class SingleList:def __init__(self, node=None):self._head = nodedef isEmpty(self):return self._head == Nonedef append(self, item):# 尾部添加node = Node(item)if self.isEmpty():self._head = nodeelse:cur = self._headwhile cur.next != None:cur = cur.nextcur.next = node# 求長度def len(self):cur = self._headcount = 0while cur != None:count += 1cur = cur.nextreturn count# 遍歷def print_all(self):cur = self._headwhile cur != None:print(cur)cur = cur.nextdef pop(self, index):if index < 0 or index >= self.len():raise IndexError('index Error')if index == 0:self._head = self._head.nextelse:cur = self._head# 找到當前下標的前一個元素while index - 1:cur = cur.nextindex -= 1# 修改的next的指向位置cur.next = cur.next.nextdef insert(self, index, item):if index < 0 or index >= self.len():raise IndexError('index Error')if isinstance(item, Node):raise TypeError('不能是Node類型')else:node = Node(item)if index == 0:node.next = self._headself._head = nodeelse:cur = self._headwhile index - 1:cur = cur.nextindex -= 1node.next = cur.nextcur.next = nodedef update(self, index, new_item):if index < 0 or index >= self.len():raise IndexError('index Error')if isinstance(new_item, Node):raise TypeError('不能是Node類型')else:node = Node(new_item)if index == 0:node.next = self._head.nextself._head = nodeelse:cur = self._headnode.next = cur.next.nextcur.next = nodedef remove(self, item):if isinstance(item, Node):raise TypeError('不能是Node類型')else:node = Node(item)cur = self._headwhile cur == node:cur = cur.nextcur.next = cur.next.nextif __name__ == '__main__':slist = SingleList()print(slist.isEmpty()) # Trueprint(slist.len())slist.append(5)print(slist.isEmpty()) # Falseprint(slist.len()) # 1slist.append(8)slist.append(6)slist.append(3)slist.append(1)print(slist.isEmpty()) # Trueprint(slist.len())print('---------------------')slist.print_all()print('----------pop-------------')slist.pop(2)slist.print_all()print('--------insert-------')slist.insert(1, 19)slist.print_all()print('--------update-------')slist.update(1, 18)slist.print_all()print('--------remove-------')slist.remove(18)slist.print_all()
雙向鏈表:
insert、 remove、 update方法
'''
雙向鏈表
'''class Node:def __init__(self, data):self.data = dataself.next = Noneself.prev = Nonedef __str__(self):return str(self.data)class DoubleList:def __init__(self):self._head = Nonedef isEmpty(self):return self._head == Nonedef append(self, item):# 尾部添加node = Node(item)if self.isEmpty():self._head = nodeelse:cur = self._headwhile cur.next != None:cur = cur.nextcur.next = node# 求長度def add(self, item):node = Node(item)if self.isEmpty():self._head = nodeelse:node.next = self._headself._head.prev = nodeself._head = nodedef len(self):cur = self._headcount = 0while cur != None:count += 1cur = cur.nextreturn countdef print_all(self):cur = self._headwhile cur != None:print(cur)cur = cur.nextdef insert(self, index, item):if index < 0 or index >= self.len():raise IndexError('index Error')if isinstance(item, Node):raise TypeError('不能是Node類型')if index == 0:node = Node(item)node.next = self._headself._head.prev= nodeself._head = nodeelse:cur = self._headnode = Node(item)while index - 1:cur = cur.nextindex -= 1#cur 是xindex的前一個節點# 設置node節點的前一個是cur節點node.prev = cur#設置node的后一個節點node.next = cur.next#設置cur下一個節點的prev指向nodecur.next.prev = node# 設置cur的下一個節點cur.next = nodedef remove(self, item):if self.isEmpty():raise ValueError('double link list is empty')else:cur = self._headif cur.data == item:#刪除的是頭節點if cur.next ==None:#只有頭節點self._head = Noneelse:# 除了頭部節點,還有其他節點cur.next.prve = Noneself._head = cur.nextelse:while cur != None:if cur.data == item:cur.prev.next = cur.nextcur.next.prve = cur.prev # 雙向的breakcur = cur.nextdef update(self, index, new_item):if index < 0 or index >= self.len():raise IndexError('index Error')if isinstance(new_item, Node):raise TypeError('不能是Node類型')else:node = Node(new_item)cur = self._head#獲取curwhile index :cur = cur.nextindex -= 1node.prev = cur.prevcur.prev.next = nodenode.next =cur.nextcur.next.prev = node# if index == 0:# node.next = self._head.next# node.prev = self._head.prev# self._head = node# else:# cur = self._head# node.next = cur.next.next# node.prev = cur.prev# cur.next = node# cur.prev = nodeif __name__ == '__main__':dlist = DoubleList()print(dlist.len())print(dlist.isEmpty())# dlist.append(6)# dlist.append(9)# dlist.append(5)# print(dlist.len())# print(dlist.isEmpty())# dlist.print_all()dlist.add(6)dlist.add(9)dlist.add(5)dlist.print_all()print('--------insert-------')dlist.insert(1, 19)dlist.print_all()print('--------update-------')dlist.update(1, 1)dlist.print_all()print('--------remove-------')dlist.remove(9)dlist.print_all()
?