标签:== enter except 函数 rms 资源 abs div super
单链表实现多项式的加法运算
最近学习数据结构的线性表,有顺序存储和链表两种,多项式的表示和运算,最能巩固学习成果,现在提供详细代码,来实现多项式的加法运算。
多项式用单链表最为合适,不会造成更多的资源浪费。
如果你恰好用的这本书--数据结构(Java版)(第4版)(叶核亚),推荐你去下面这个链接下载书本源代码,将更助你学的轻松。
http://download.csdn.net/detail/wang______jing/9907538
1 //单链表类,实现一些基本单链表的操作 2 public class SinglyList<T> extends Object { 3 public Node<T> head; // 头指针,指向单链表的头结点 4 5 public SinglyList() { 6 this.head = new Node<T>(); 7 } 8 9 public SinglyList(T[] values) { // values数组中的值构成结点 10 this(); 11 Node<T> rear = this.head; 12 for (int i = 0; i < values.length; i++) { 13 rear.next = new Node<T>(values[i], null); 14 rear = rear.next; 15 } 16 } 17 18 public boolean isEmpty() { 19 return this.head.next == null; 20 } 21 22 public T get(int i) { 23 Node<T> p = this.head.next; 24 for (int j = 0; p != null && j < i; j++) { 25 p = p.next; 26 } 27 return (i >= 0 && p != null) ? p.data : null; 28 } 29 30 public void set(int i, T x) { 31 if (i < 0 || i > size()) 32 throw new IndexOutOfBoundsException(i + ""); 33 if (x == null) 34 throw new NullPointerException("x==null"); 35 Node<T> p = this.head.next; 36 for (int j = 0; p != null && j < i; j++) { 37 p = p.next; 38 } 39 p.data = x; 40 41 } 42 43 public int size() { 44 int len = 0; 45 Node<T> p = this.head.next; 46 if (p == null) 47 return -1; 48 while (p != null) { 49 len++; 50 p = p.next; 51 52 } 53 return len; 54 } 55 56 public Node<T> insert(int i, T x) { 57 if (x == null) 58 throw new NullPointerException("x==null"); 59 Node<T> front = this.head; 60 for (int j = 0; front.next != null && j < i; j++) { 61 front = front.next; 62 } 63 front.next = new Node<T>(x, front.next); 64 return front.next; 65 66 } 67 68 public Node<T> insert(T t) { 69 return insert(Integer.MAX_VALUE, t); 70 } 71 72 public T remove(int i) { 73 Node<T> front = this.head; 74 for (int j = 0; front.next != null && j < i; j++) { 75 front = front.next; 76 } 77 if (i >= 0 && front.next != null) { 78 T old = front.next.data; 79 front.next = front.next.next; 80 return old; 81 } 82 return null; 83 84 } 85 86 public void clear() { 87 this.head.next = null; 88 } 89 90 } 91 // 排序单链表,对于一些结点可以实现按顺序插入排序 92 public class SortedSinglyList<T extends Comparable<? super T>> extends 93 SinglyList<T> { 94 public SortedSinglyList() { 95 super(); 96 } 97 98 public SortedSinglyList(T[] values) { // values的数组中的对象按值插入 99 super(); 100 for (int i = 0; i < values.length; i++) { 101 this.insert(values[i]); 102 } 103 } 104 105 public SortedSinglyList(SinglyList<T> list) { // 单链表的深拷贝 106 super(); 107 for (Node<T> p = list.head.next; p != null; p = p.next) 108 this.insert(p.data); 109 } 110 111 public Node<T> insert(T x) { // 覆盖父类的insert方法 112 Node<T> front = this.head, p = front.next; 113 while (p != null && x.compareTo(p.data) > 0) { 114 front = p; 115 p = p.next; 116 } 117 front.next = new Node<T>(x, p); // 在front之后,p之前插入x节点 118 return front.next; 119 } 120 121 } 122 123 //可相加接口 124 public interface Addible<T> { 125 public void add(T t); // 两元素相加 126 127 public boolean removable(); // 删除元素 128 129 } 130 //比较大小接口 131 public interface Comparable<T> { 132 int compareTo(T obj); 133 134 } 135 136 /* 137 * 一元多项式节点类 存储多项式的系数(coefficient)和指数(exponential) 138 */ 139 public class PolyNode implements Comparable<PolyNode>, Addible<PolyNode> { 140 private int coef;// 系数 141 private int expo;// 指数 142 143 protected PolyNode next; 144 145 public PolyNode() { 146 this(0, 0); 147 } 148 149 public PolyNode(int coef, int expo) { // 带参数的构造函数 150 151 this.coef = coef; 152 this.expo = expo; 153 // this.next = null; 154 } 155 156 public PolyNode(PolyNode po) { // 拷贝构造函数 157 this(po.coef, po.expo); 158 } 159 160 public boolean equals(Object obj) // 比较两项是否相等 161 { 162 if (this == obj) 163 return true; 164 if (!(obj instanceof PolyNode)) 165 return false; 166 PolyNode term = (PolyNode) obj; 167 return this.coef == term.coef && this.expo == term.expo; 168 } 169 170 public PolyNode(String str) { // 参数为一个项时,得到项的系数和指数 171 if (str.charAt(0) == ‘+‘) 172 str = str.substring(1); // 去掉‘+’号 173 int i = str.indexOf(‘x‘); 174 if (i == -1) { 175 this.coef = Integer.parseInt(str); // 没有x,指数就为0 176 this.expo = 0; 177 } else { 178 if (i == 0) { // x在开头,系数为1 179 this.coef = 1; 180 } else { 181 String str1 = str.substring(0, i);// 截取x符号之前的系数 182 if (str1.equals("-")) { // 为‘-’,系数为-1 183 this.coef = -1; 184 } else { 185 this.coef = Integer.parseInt(str1); 186 } 187 i = str.indexOf(‘^‘);// 找^符号 188 if (i == -1) 189 this.expo = 1; 190 else 191 this.expo = Integer.parseInt(str.substring(i + 1)); 192 } 193 } 194 } 195 196 public String toString() { // 显示多项式的样式 197 String sy = this.coef > 0 ? "+" : "-"; 198 if (this.expo == 0 || this.expo > 0 && this.coef != 1 199 && this.coef != -1) { 200 sy += Math.abs(this.coef); // 系数绝对值,系数为-1或1不能显示 201 } 202 if (this.expo > 0) 203 sy += "x";// 指数为0不能输出x 204 if (this.expo > 1) 205 sy += "^" + this.expo;// 指数为1不能输出^ 206 return sy; 207 } 208 209 public int compareTo(PolyNode a) { // 比较两项的大小 210 return (this.expo < a.expo ? -1 : (this.expo == a.expo ? 0 : 1)); 211 } 212 213 public void add(PolyNode po) { // 相加 214 if (this.compareTo(po) == 0) 215 this.coef += po.coef; 216 217 } 218 219 public boolean removable() { // 删除 220 return this.coef == 0; 221 } 222 223 public int getCoef() { 224 return coef; 225 } 226 227 public void setCoef(int coef) { 228 this.coef = coef; 229 } 230 231 public int getExpo() { 232 return expo; 233 } 234 235 public void setExpo(int expo) { 236 this.expo = expo; 237 } 238 239 } 240 /* 241 * 一元多项式排序链表类,提供排序单链表的多项式相加运算; 242 */ 243 public class PolySort<T extends Comparable<? super T> & Addible<T>> extends 244 SortedSinglyList<T> { 245 246 public PolySort() { 247 super(); // 构造空单链表 248 } 249 250 public PolySort(T value[]) { // 构造方法,项的组来指定各项的值 251 super(value); 252 } 253 254 public PolySort(PolySort<T> list) {// 调用父类的深拷贝函数,复制所有结点 255 super(list); 256 } 257 258 // 多项式相加函数 259 public void addAll(PolySort<T> list) { 260 Node<T> front = this.head, p = front.next; 261 Node<T> q = list.head.next; 262 while (p != null && q != null) { 263 if (p.data.compareTo(q.data) == 0) { // 两项大小相同 264 p.data.add(q.data); 265 if (p.data.removable()) { // 删除系数为0的结点 266 front.next = p.next; 267 p = front.next; 268 } else { 269 front = p; 270 p = p.next; 271 272 } 273 q = q.next; 274 } else if (p.data.compareTo(q.data) < 0) { 275 front = p; 276 p = p.next; 277 } else { 278 front.next = new Node<T>(q.data, p); 279 q = q.next; 280 } 281 } 282 while (q != null) // 将list单链表中剩余结点复制并插入到当前链表尾 283 { 284 front.next = new Node<T>(q.data, null); 285 front = front.next; 286 q = q.next; 287 } 288 } 289 290 } 291 // 多项式类,使用多项式排序单链表类作为成员变量,提供多项式相加运算 292 public class Polynomial { 293 private PolySort<PolyNode> list; // 多项式排序单链表 294 295 public Polynomial() // 构造方法 296 { 297 this.list = new PolySort<PolyNode>(); // 创建空单链表,执行排序单链表默认构造方法 298 } 299 300 public Polynomial(PolyNode terms[]) // 构造方法,由项数组指定多项式各项值 301 { 302 this.list = new PolySort<PolyNode>(terms); 303 } 304 305 public Polynomial(String polystr) // 构造方法,参数指定多项式表达式字符串 306 { 307 this(); 308 if (polystr == null || polystr.length() == 0) 309 return; 310 Node<PolyNode> rear = this.list.head; 311 int start = 0, end = 0; // 序号start~end的子串为一项 312 while (start < polystr.length() && end < polystr.length()) { 313 int i = polystr.indexOf(‘+‘, end + 1); // 返回字符+在字符串中从end+1开始的序号 314 if (i == -1) // 未找到指定字符 315 i = polystr.length(); 316 int j = polystr.indexOf(‘-‘, end + 1); 317 if (j == -1) 318 j = polystr.length(); 319 end = i < j ? i : j; // end为下一个+或-号的序号 320 rear.next = new Node<PolyNode>(new PolyNode(polystr.substring( 321 start, end)), null); 322 // 尾插入,以序号start~end的子串作为一项,创建结点,创建元素对象 323 rear = rear.next; 324 start = end; 325 } 326 } 327 328 public Polynomial(Polynomial poly) // 深度拷贝构造方法,复制所有结点和对象 329 { 330 this(); // 创建空单链表,只有头结点 331 Node<PolyNode> rear = this.list.head; 332 for (Node<PolyNode> p = poly.list.head.next; p != null; p = p.next) // p遍历poly单链表 333 { 334 rear.next = new Node<PolyNode>(new PolyNode(p.data), null);// 复制结点,复制对象 335 rear = rear.next; 336 } 337 } 338 339 public String toString() // 返回多项式的描述字符串 340 { 341 String str = ""; 342 for (Node<PolyNode> p = this.list.head.next; p != null; p = p.next) 343 str += p.data.toString(); 344 return str; 345 } 346 347 public void addAll(Polynomial poly) // 多项式相加,this+=poly 348 { 349 this.list.addAll(poly.list); 350 } 351 352 public Polynomial union(Polynomial poly) // 加法+,C=this+poly 353 { 354 Polynomial polyc = new Polynomial(this); // 深度拷贝,复制所有结点和对象 355 polyc.addAll(poly); // polyc+=poly 356 return polyc; // 返回对象引用 357 } 358 359 public boolean equals(Object obj) // 比较两个多项式是否相等 360 { 361 return this == obj || obj instanceof Polynomial 362 && this.list.equals(((Polynomial) obj).list); 363 // 比较两条单链表是否相等 364 } 365 } 366 // 测试类,完成多项式的相加结果并显示 367 public class Poly extends PolySort { 368 369 public static void main(String[] args) { 370 371 System.out.println("//一元多项式"); 372 PolyNode aterms[] = { new PolyNode(-7, 9), new PolyNode(2, 7), 373 new PolyNode(-9, 4), new PolyNode(1, 2), new PolyNode(-1, 1), 374 new PolyNode(2, 0) }; 375 Polynomial apoly = new Polynomial(aterms); 376 377 PolyNode bterms[] = { new PolyNode(9, 11), new PolyNode(5, 10), 378 new PolyNode(-3, 8), new PolyNode(10, 4), new PolyNode(-1, 2), 379 new PolyNode(1, 1), new PolyNode(-1, 0) }; 380 Polynomial bpoly = new Polynomial(bterms); 381 382 // Polynomial bpoly = new Polynomial("-1+x-x^2+10x^4-3x^8+5x^10+9x^11"); 383 System.out.println("A=" + apoly.toString() + "\nB=" + bpoly.toString()); 384 385 Polynomial cpoly = apoly.union(bpoly); 386 System.out.println("C=A+B,C=" + cpoly.toString()); 387 apoly.addAll(bpoly); 388 System.out.println("A+=B,A=" + apoly.toString()); 389 390 } 391 }
标签:== enter except 函数 rms 资源 abs div super
原文地址:http://www.cnblogs.com/wang118/p/7222522.html