图示说明:
单向链表:
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()