链表多项式为什么我的输出只打印第一个项。

huangapple go评论105阅读模式
英文:

Linkedlist polynomial why my output only print first term

问题

我刚刚编写了一个程序,用于添加两个多项式链接列表。我的输出只打印出多项式的第一个项。

我无法弄清楚有什么错误。我想在这里得到一些帮助。有人可以告诉我代码有什么问题吗?

  1. public void addNode(int cof, int exp) {
  2. PNode node = new PNode(cof, exp);
  3. if (first == null) {
  4. first = last = node;
  5. return;
  6. }
  7. last.next = node;
  8. last = node;
  9. }
  10. public PolynomialLinkedList add(PolynomialLinkedList s) {
  11. PolynomialLinkedList sum = new PolynomialLinkedList();
  12. // 实现这个方法
  13. PNode list1 = first;
  14. PNode list2 = s.first;
  15. while (list1 != null && list2 != null) {
  16. if (list1.exp == list2.exp) {
  17. sum.addNode(list1.coe + list2.coe, list1.exp);
  18. list1 = list1.next;
  19. list2 = list2.next;
  20. } else if (list1.exp > list2.exp) {
  21. sum.addNode(list1.coe, list1.exp);
  22. list1 = list1.next;
  23. } else if (list2.exp > list1.exp) {
  24. sum.addNode(list2.coe, list2.exp);
  25. list2 = list2.next;
  26. }
  27. }
  28. return sum;
  29. }

(注意:此处只提供翻译后的代码,不包含额外内容。)

英文:

I just wrote a program that add two polynomial linked-list. my output only prints first term of the polynomials.

I cannot figure out what bug is. I want to get some help here. could someone tell what's wrong with my code.

  1. public void addNode(int cof, int exp) {
  2. PNode node = new PNode(cof, exp);
  3. if (first == null) {
  4. first = last = node;
  5. return;
  6. }
  7. last.next = node;
  8. last = node;
  9. }
  10. public PolynomialLinkedList add(PolynomialLinkedList s) {
  11. PolynomialLinkedList sum = new PolynomialLinkedList();
  12. //implement this method
  13. PNode list1 = first;
  14. PNode list2 = s.first;
  15. while (list1 != null && list2 != null) {
  16. if (list1.exp == list2.exp) {
  17. sum.addNode(list1.coe + list2.coe, list1.exp);
  18. list1 = list1.next;
  19. list2 = list2.next;
  20. } else if (list1.exp > list2.exp) {
  21. sum.addNode(list1.coe, list1.exp);
  22. list1 = list1.next;
  23. } else if (list2.exp > list1.exp) {
  24. sum.addNode(list2.coe, list2.exp);
  25. list2 = list2.next;
  26. }
  27. }
  28. return sum;
  29. }

答案1

得分: 2

如果其中一个列表先结束,其他列表中的剩余项目将被忽略,不会被添加到总和中。

添加额外的循环

  1. while(list1 != null) {
  2. sum.addNode(list1.coe, list1.exp);
  3. list1 = list1.next;
  4. }

  1. while(list2 != null) {
  2. sum.addNode(list2.coe, list2.exp);
  3. list2 = list2.next;
  4. }
英文:

If one of the lists finishes first, the rest of the items in other list are ignored and are not getting added to the sum.

Add additional loops

  1. while(list1 != null) {
  2. sum.addNode(list1.coe,list1.exp);
  3. list1=list1.next;
  4. }

and

  1. while(list2 != null) {
  2. sum.addNode(list2.coe,list2.exp);
  3. list2=list2.next;
  4. }

huangapple
  • 本文由 发表于 2020年9月15日 02:44:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63890165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定