1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
''' class Q(): Data[MaxSize] rear front '''
class Queue(): def __init__(self,MaxSize): self.size = MaxSize self.rear = 0 self.front = 0 self.Data = [None] * self.size def add(self,item): if (self.rear+1)%self.size == self.front: print('队列已经满了,添加%s失败'%item) return self.rear += 1 self.Data[self.rear] = item def out(self): if self.rear == self.front: print('队列为空') self.front = (self.front+1)%self.size out_item = self.Data[self.front] self.Data[self.front] = None return out_item
q = Queue(3) q.add(9) q.add(10) q.add(11) print(q.Data) print(q.out()) print(q.Data)
class Node(): def __init__(self,data): self.data = data self.next = None class Qlinckedlist(): def __init__(self): self.rear = None self.front = None def out(self): if self.front == None: print('队列为空。') if self.front == self.rear: self.rear = None temp = self.front self.front = temp.next return temp.data def add(self,item): node = Node(item) if self.front == None: self.front = node self.rear = node else: self.rear.next = node self.rear = self.rear.next def show(self): linckedlist = [] temp = self.front while temp: (linckedlist.append(temp.data)) temp = temp.next print(linckedlist) queue = Qlinckedlist() queue.add(12) queue.add('x') queue.add('y') queue.add(88) queue.add(99) queue.show() queue.out() queue.out() queue.show()
'''
# p1\p2分别指向两个多项式的第一个节点,
若:p1.expon==p2.expon,系数相加,若结果!=0,作为结果多项式的系数, p1、p2都向后移动一项。 若:p1.expon>p2.expon,p1结果放进结果多项式,p1后移一位。 若:p1.expon<p2.expon,p2结果放进结果多项式,p2后移一位。 某一边的多项式挪完了,将另一个多项式剩余节点依次放进结果多项式中。 ''' class PolyNode(): def __init__(self,coef,expon): self.coef = coef self.expon = expon self.next = None
a = [[5,2],[10,3],[2,-2],[9,4]] b = [[3,4],[5,7],[-10,2]] class Creat_linkedlist(): def __init__(self,multlist): self.front = None self.rear = None self.multlist = sorted(multlist,key=lambda x:x[1],reverse=True) self.multadd() self.show() def add(self,c,e): node = PolyNode(c,e) if self.front == None: self.front = node self.rear = node else: self.rear.next = node self.rear = self.rear.next def show(self): linckedlist = [] temp = self.front while temp: (linckedlist.append([temp.coef,temp.expon])) temp = temp.next print(linckedlist) def multadd(self): for data in self.multlist: self.add(data[0],data[1])
def add_computing(a_linkedlist,b_linkedlist): c_linkedlist = Creat_linkedlist([[0, 0]]) p1 = a_linkedlist.front p2 = b_linkedlist.front while(p1 and p2): if p1.expon == p2.expon: sum = p1.coef + p2.coef if sum: Attach(sum,p1.expon,c_linkedlist) p1 = p1.next p2 = p2.next continue elif p1.expon < p2.expon: Attach(p2.coef,p2.expon,c_linkedlist) p2 = p2.next continue else: Attach(p1.coef,p1.expon,c_linkedlist) p1 = p1.next continue while p1: Attach(p1.coef,p1.expon,c_linkedlist) p1 = p1.next while p2: Attach(p2.coef, p2.expon, c_linkedlist) p2 = p2.next c_linkedlist.front = c_linkedlist.front.next return c_linkedlist
def Attach(c,e,linkedlist): p = PolyNode(c,e) linkedlist.rear.next = p linkedlist.rear = linkedlist.rear.next
a_linkedlist = Creat_linkedlist(a) b_linkedlist = Creat_linkedlist(b) c_linkedlist = add_computing(a_linkedlist,b_linkedlist) c_linkedlist.show()
''' 多项式求积算法思路: 逐项插入 p1的第一项乘p2的第一项,指数相加,系数相乘,,插入新链表,以此类推 关键在于如何降序插入。可以用左插入和右插入方法, 也可以用 一个移动的游标。利用游标逐个比较找到插入点。 重新回顾以下链表的基本遍历框架 while p: balaba...labala p = p.next 尤其需要注意,链表的递归嵌套易错点: 正确案例如下: t1 = a_linkedlist.front while t1: t2 = b_linkedlist.front # 让t2 循环一次后 ,能复位到t2 头指针位置。 while t2: print(t1.expon,t2.expon) t2 = t2.next t1 = t1.next 易错写成: t1 = a_linkedlist.front t2 = b_linkedlist.front # 这样嵌套的循环一次结束后,无法复位到t2 的头指针位置。 while t1: while t2: print(t1.expon,t2.expon) t2 = t2.next t1 = t1.next ''' def mult(alinkedlist,blinkedlist): t1 = alinkedlist.front new_linkedlist = Creat_linkedlist([[0, 0]]) while t1: t2 = blinkedlist.front while t2: c = t1.coef*t2.coef e = t1.expon+t2.expon temp = new_linkedlist.front while temp.next and temp.next.expon > e: temp = temp.next if temp.next and temp.next.expon == e: if (temp.next.coef + c) == 0: temp.next = temp.next.next else: temp.next.coef = temp.next.coef + c print(temp.next.coef,c) else: p = PolyNode(c,e) p.next = temp.next temp.next = p t2 = t2.next t1 = t1.next new_linkedlist.front = new_linkedlist.front.next return new_linkedlist
d_linkedlist = mult(a_linkedlist,b_linkedlist) d_linkedlist.show()
|