python 数据结构 --使用python链表实现有序表
有序链表
概述
有序表依据数据项的可比性质(如整数大小,字母表前后)来决定数据项在列表中的位置。
比如下面我们要实现数字按照大小排列的操作。
有序表中的操作:
- OrderedList():创建一个新的空有序列表。它返回一个空有序列表并且不需要传递任何参数。
- add(item):在保持原有顺序的情况下向列表中添加一个新的元素,新的元素作为参数传递进函数而函数无返回值。假设列表中原先并不存在这个元素。
- remove(item):从列表中删除某个元素。欲删除的元素作为参数,并且会修改原列表。假设原列表
- 中存在欲删除的元素。
- search(item):在列表中搜索某个元素,被搜索元素作为参数,返回一个布尔值。
- isEmpty():测试列表是否为空,不需要输入参数并且其返回一个布尔值。
- size():返回列表中元素的数量。不需要参数,返回一个整数。
- index(item):返回元素在列表中的位置。需要被搜索的元素作为参数输入,返回此元素的索引值。假设这个元素在列表中。
- pop():删除并返回列表中的最后一项。不需要参数,返回删除的元素。假设列表中至少有一个元素。
- pop(pos):删除并返回索引 pos 指定项。需要被删除元素的索引值作为参数,并且返回这个元素。假设该元素在列表中。
或不多说直接上代码:
首先需要定义链表中的节点类:单独写成一个py: Node.py 定义的类实现了节点的数据项data与节点的指向next
1 class Node: 2 def __init__(self, initial): 3 self.data = initial 4 self.next = None 5 6 def getData(self): 7 return self.data 8 9 def getNext(self): 10 return self.next 11 12 def setData(self,newdata): 13 self.data = newdata 14 15 def setNext(self,newnext): 16 self.next = newnext
定义好Node后就可以引入Node来实现Orderlist: OrderList.py:
1 from Node import Node 2 3 class OrderList: 4 ''' 5 采用链表实现有序表 \n 6 Node定义不变 \n 7 设置一个head保存链表表头 \n 8 OrderedList():创建一个新的空有序列表。它返回一个空有序列表并且不需要传递任何参数。\n 9 add(item):在保持原有顺序的情况下向列表中添加一个新的元素,新的元素作为参数传递进函数而函数无返回值。假设列表中原先并不存在这个元素。\n 10 remove(item):从列表中删除某个元素。欲删除的元素作为参数,并且会修改原列表。假设原列表中存在欲删除的元素。\n 11 search(item):在列表中搜索某个元素,被搜索元素作为参数,返回一个布尔值。\n 12 isEmpty():测试列表是否为空,不需要输入参数并且其返回一个布尔值。\n 13 size():返回列表中元素的数量。不需要参数,返回一个整数。\n 14 index(item):返回元素在列表中的位置。需要被搜索的元素作为参数输入,返回此元素的索引值。假设这个元素在列表中。\n 15 pop():删除并返回列表中的最后一项。不需要参数,返回删除的元素。假设列表中至少有一个元素。\n 16 pop(pos):删除并返回索引 pos 指定项。需要被删除元素的索引值作为参数,并且返回这个元素。假设该元素在列表中。\n 17 ''' 18 def __init__(self): 19 self.head = None 20 21 def search(self, item): 22 current = self.head 23 found = False 24 stop = False 25 while current != None and not found and not stop: 26 if current.getData() == item: 27 found = True 28 else: 29 if current.getData() > item: 30 stop = True 31 else: 32 current = current.getNext() 33 return found 34 35 ''' 36 总结,链表的各种实现: 37 1、首先在定义函数时,创建一个current来表示指向表头 38 2、对于查找问题,在进入循环前,创建好决定状态的量(停止与否) 39 3、在循环内实现current的指向变化,一些数据操作等 40 ''' 41 42 def add(self, item): 43 current = self.head 44 previous = None 45 stop = None 46 while current != None and not stop: #首先发现插入位置 47 if current.getData() > item: 48 stop = True 49 else: 50 previous = current 51 current = current.getNext() 52 53 temp = Node(item) #创建Node 54 if previous is None: #判断是插入表头还是插入表中 55 temp.setNext(self.head) 56 self.head = temp 57 else: 58 temp.setNext(current) 59 previous.setNext(temp) 60 61 def size(self): 62 current = self.head 63 n = 0 64 while current is not None: 65 n = n + 1 66 current = current.getNext() 67 return n 68 69 def isEmpty(self): 70 return self.head == None 71 72 def index(self, item): 73 current = self.head 74 found = False 75 n = 0 76 while current != None and not found: 77 if current.getData() == item: 78 found = True 79 else: 80 current = current.getNext() 81 n = n + 1 82 return n 83 84 def remove(self, item): 85 current = self.head 86 previous = None 87 found = False 88 while current != None and not found: 89 if current.getData() == item: 90 found = True 91 else: 92 previous = current 93 current = current.getNext() 94 95 if previous == None: 96 self.head = current.getNext() 97 else: 98 previous.setNext(current.getNext()) 99 100 def pop(self): 101 current = self.head 102 previous = None #创建previous以获得最后一个NOde的前一个node 103 while current.getNext() != None: #判断下一个node是否存在 104 previous = current #获得最后一个NOde的前一个node 105 current = current.next #遍历整个表直到最后 106 previous.setNext(current.getNext()) #将previous的next设为current的下一个,即删除current node 107 return current.getData() 108 109 def pop_index(self, pop): 110 current = self.head 111 previous = None 112 found = False 113 n = 0 114 while current != None and not found: 115 if n == pop: 116 found = True 117 else: 118 previous = current 119 current= current.getNext() 120 n = n + 1 121 if previous == None: 122 self.head = current.getNext() 123 else: 124 previous.setNext(current.getNext()) 125 return current.getData() 126 127 guss = OrderList() 128 guss.add(10) 129 guss.add(1) 130 guss.add(21) 131 guss.add(18) 132 print(guss.search(10)) 133 print(guss.search(4)) 134 print(guss.isEmpty()) 135 print(guss.index(18)) 136 print(guss.size()) 137 guss.remove(18) 138 print(guss.size()) 139 print(guss.pop()) 140 print(guss.size()) 141 print(guss.pop_index(0)) 142 print(guss.size())
运行的结果如下:
True
False
False
2
4
3
21
2
1
1