简单的实现了链表的增删改查功能
单链表
class Node(object): def __init__(self, data = -1, next = None): self.data = data self.next = next class List(object): def __init__(self, head=Node(-1)): self.head = head self.length = 0 def __del__(self): del self.head self.length = 0 @classmethod # 看了 装饰器才懂, 这会记忆化!!! def create(cls): obj = cls() return obj @staticmethod def printf(head) -> bool: if head == None: return False tmp = head while tmp.next != None: tmp = tmp.next print(tmp.data, end='') return True def insert(self, index, val) -> bool: # 位置 & 值 if self.head == None or index < 0 or index > self.size(): return False tmp = self.head while index > 0: tmp = tmp.next index -= 1 node = Node(val, tmp.next) tmp.next = node self.length += 1 return True def delete(self, index) -> bool: # 位置 if self.head == None or index < 0 or index >= self.size(): return False tmp = self.head while index > 0: tmp = tmp.next index -= 1 tmp.next = tmp.next.next self.length -= 1 return True def modify(self, index, val) -> bool: # 位置 & 值 if self.head == None or index < 0 or index >= self.size(): return False tmp = self.head while index >= 0: tmp = tmp.next index -= 1 tmp.data = val return True def find(self, val) -> int: # return 位置 tmp = self.head now = 0 while tmp.next != None: tmp = tmp.next if tmp.data == val: return now now += 1 return -1 def size(self) -> int: return self.length def empty(self) -> bool: return self.head.next == None if __name__ == '__main__': lis = List.create() lis.insert(0, 1) lis.insert(0, 2) lis.insert(1, 3) lis.insert(0, 4) lis.delete(3) List.printf(lis.head) lis.find(2)
双链表
class Node(object): def __init__(self, val): self.val = val self.pre = None self.next = None class List(object): def __init__(self, head = Node(-1), tail = Node(-1)): self.head = head self.tail = tail head.next = tail tail.pre = head self.length = 0 def __del__(self): del self.head del self.tail self.length = 0 def insert(self, index, val): if index < 0 or index > self.size() or self.head == None: return False tmp = Node(val) cur = self.head while index > 0: cur = cur.next index -= 1 tmp.next = cur.next cur.next = tmp tmp.next.pre = tmp tmp.pre = cur self.length += 1 return True def delete(self, index): if index < 0 or index >= self.size() or self.head == None: return False cur = self.head while index > 0: cur = cur.next index -= 1 cur.next = cur.next.next cur.next.pre = cur self.length -= 1 return True def modify(self, index, val): if index < 0 or index >= self.size() or self.head == None: return False cur = self.head while index >= 0: cur = cur.next index -= 1 cur.val = val return True def find(self, val): if self.head == None: return -1 cur = self.head cnt = 0 while cur.next != self.tail: cur = cur.next if cur.val == val: return cnt cnt += 1 return -1 def printf(self): cur = self.head while cur.next != self.tail: cur = cur.next print(cur.val, end='') def size(self): return self.length def empty(self): return size() == 0 if __name__ == '__main__': lis = List() lis.insert(0, 1) lis.insert(0, 2) lis.insert(2, 3) lis.insert(1, 4) # 2413 lis.printf() lis.delete(2) lis.delete(2) print() lis.printf() lis.modify(1, 0) print() lis.printf() lis.find(4)
主要熟悉下python的数据结构写法,bug应该存在,不想调(~hhh~