顺序表
python中的list和tuple就采用了顺序表的实现技术
tuple是不变的表,因此不支持改变其内部状态的任何操作
list是一种元素个数可变的线性表,可以加入和删除元素,在各种操作中维持已有元素的顺序
list对象无法设置容量的操作
if __name__ == '__main__': lt = list([1, 3, 2]) print(len(lt)) # 3 print(lt) # [1, 3, 2] # 元素顺序倒置 lt.reverse() print(lt) # [2, 3, 1] # 排序 lt.sort() print(lt) # [1, 2, 3] # 清除表中所有元素 lt.clear() print(len(lt)) # 0
单链表:
单链表头部添加元素:
class LNode: """表节点""" # next_防止与标准函数next重名 def __init__(self, elem, next_=None): self.elem = elem self.next = next_ class LinkedListUnderflow(ValueError): pass class LList: """单链表""" def __init__(self): self._head = None def is_empty(self): """链表是否为空""" return self._head is None def __len__(self): """链表长度""" count = 0 p = self._head while p: count += 1 p = p.next return count def prepend(self, elem): """表头插入数据""" # node = LNode(elem) # node.next = self._head # self._head = node # 合并为一句 self._head = LNode(elem, self._head) def pop(self): """删除头一个元素""" if self._head is None: # 无结点,抛出异常 raise LinkedListUnderflow("in pop") e = self._head.elem self._head = self._head.next return e def append(self, elem): """尾部添加元素""" if self._head is None: self._head = LNode(elem) return p = self._head while p.next is not None: p = p.next p.next = LNode(elem) def printall(self): p = self._head while p: print(p.elem, end='') if p.next: print(', ', end='') p = p.next print('') def for_each(self, proc): """遍历函数,proc的参数应该是可以作用于表元素的操作函数,如:print""" p = self._head while p: proc(p.elem) p = p.next def elements(self): """迭代器""" p = self._head while p: yield p.elem p = p.next def find(self, pred): """返回符合条件的第一个""" p = self._head while p is not None: if pred(p.elem): return p.elem p = p.next def filter(self, pred): """筛选生成器""" p = self._head while p is not None: if pred(p.elem): yield p.elem p = p.next if __name__ == '__main__': llist1 = LNode(1) p = llist1 for i in range(2, 11): p.next = LNode(i) p = p.next p = llist1 # 可简写 while p: while p is not None: print(p.elem) p = p.next mlist1 = LList() for i in range(3): mlist1.prepend(i) for i in range(4, 7): mlist1.append(i) mlist1.printall() # 2, 1, 0, 4, 5, 6 mlist1.for_each(print) print("-------") for x in mlist1.elements(): print(x, end=' ') # 2 1 0 4 5 6 print("-------") print(mlist1.find(lambda z: z > 3)) # 4 print("-------") for x in mlist1.filter(lambda z: z > 3): print(x, end=' ') # 4 5 6 print("-------") print(len(mlist1)) # 6
带有尾节点引用的单链表:
头部添加元素:
class LList2(LList): """带有尾节点引用的单链表""" def __init__(self): # 相当于LList.__init__() super(LList2, self).__init__() self._rear = None def prepend(self, elem): """表头插入数据""" if self._head is None: self._head = LNode(elem, self._head) self._rear = self._head else: self._head = LNode(elem, self._head) def append(self, elem): """尾部添加元素""" if self._head is None: self._head = LNode(elem, self._head) self._rear = self._head else: self._rear.next = LNode(elem) self._rear = self._rear.next def pop_last(self): """弹出末尾元素""" if self._head is None: raise LinkedListUnderflow("in pop_last") p = self._head # 只有一个元素 # 表判断空用_head的值,所有删除最后一个结点使表变空时,不需要给_rear赋值None if p.next is None: e = p.elem self._head = None return e while p.next.next: p = p.next e = p.next.elem p.next = None self._rear = p return e if __name__ == '__main__': mlist1 = LList2() mlist1.prepend(1) mlist1.prepend(2) for i in range(3, 5): mlist1.append(i) for i in mlist1.elements(): print(i, end=' ') # 2 1 3 4 print("---------") print(mlist1.pop_last()) # 4