添加多项式(链接列表)......错误帮助

时间:2010-03-29 01:36:30

标签: java

我编写了一个程序来创建节点,在这个类中是多项式的一部分,然后将两个多项式相加成一个多项式(节点列表)。我的所有代码编译所以我遇到的唯一问题是节点没有通过我在polynomial.java中的insert方法插入到多项式中,并且在运行程序时它确实创建了节点并以2x ^ 2格式显示它们但是当把多项式加在一起时,它会显示o作为多项式,所以如果有人能弄清楚什么是错的,我可以做些什么来修复它,我将不胜感激。

以下是代码:

import java.util.Scanner;

class Polynomial{

    public termNode head;

    public Polynomial()
    {
        head = null;
    }

    public boolean isEmpty()
    {
        return (head == null);
    }

    public void display()
    {
        if (head == null)
            System.out.print("0");
        else
            for(termNode cur = head; cur != null; cur = cur.getNext())
                {
                    System.out.println(cur);
                }
    }

    public void insert(termNode newNode)
    {
        termNode prev = null;
        termNode cur = head;
        while (cur!=null && (newNode.compareTo(cur)<0))
            {
                prev = null;
                cur = cur.getNext();
            }
        if (prev == null)
            {
                newNode.setNext(head);
                head = newNode;
            }
        else
            {
                newNode.setNext(cur);
                prev.setNext(newNode);
            }
}
 public void readPolynomial(Scanner kb)
    {
        boolean done = false;
        double coefficient;
        int exponent;
        termNode term;
        head = null; //UNLINK ANY PREVIOUS POLYNOMIAL
        System.out.println("Enter 0 and 0 to end.");
        System.out.print("coefficient: ");
        coefficient = kb.nextDouble();
        System.out.println(coefficient);
        System.out.print("exponent: ");
        exponent = kb.nextInt();
        System.out.println(exponent);
        done = (coefficient == 0 && exponent == 0);
        while(!done)
            {
                Polynomial poly = new Polynomial();
                term = new termNode(coefficient,exponent);
                System.out.println(term);
                poly.insert(term);

                System.out.println("Enter 0 and 0 to end.");
                System.out.print("coefficient: ");
                coefficient = kb.nextDouble();
                System.out.println(coefficient);
                System.out.print("exponent: ");
                exponent = kb.nextInt();
                System.out.println(exponent);
                done = (coefficient==0 && exponent==0);
            }
    }
public static Polynomial add(Polynomial p, Polynomial q)
    {
        Polynomial r = new Polynomial();
        double coefficient;
        int exponent;
        termNode first = p.head;
        termNode second = q.head;
        termNode sum = r.head;
        termNode term;
        while (first != null && second != null)
            {
                if (first.getExp() == second.getExp())
                    {
                        if (first.getCoeff() != 0 && second.getCoeff() != 0);
                        {
                            double addCoeff = first.getCoeff() + second.getCoeff();
                            term = new termNode(addCoeff,first.getExp());
                            sum.setNext(term);
                            first.getNext();
                            second.getNext();
                        }
                    }
                else if (first.getExp() < second.getExp())
                    {
                        sum.setNext(second);
                        term = new termNode(second.getCoeff(),second.getExp());
                        sum.setNext(term);
                        second.getNext();
                    }
                else
 {
                        sum.setNext(first);
                        term = new termNode(first.getNext());
                        sum.setNext(term);
                        first.getNext();
                    }
            }
        while (first != null)
            {
                sum.setNext(first);
            }
        while (second != null)
            {
                sum.setNext(second);
            }
        return r;
    }
}

这是我的Node类:

class termNode implements Comparable
{
    private int exp;
    private double coeff;
    private termNode next;

    public termNode(double coefficient, int exponent)
    {
        coeff = coefficient;
        exp = exponent;
        next = null;
    }

    public termNode(termNode inTermNode)
    {
        coeff = inTermNode.coeff;
        exp = inTermNode.exp;
    }

    public void setData(double coefficient, int exponent)
    {
        coefficient = coeff;
        exponent = exp;
    }

    public double getCoeff()
    {
        return coeff;
    }

    public int getExp()
    {
        return exp;
    }

    public void setNext(termNode link)
    {
        next = link;
    }

    public termNode getNext()
    {
        return next;
    }
 public String toString()
    {
        if (exp == 0)
            {
                return(coeff + " ");
            }
        else if (exp == 1)
            {
                return(coeff + "x");
            }
        else
            {
                return(coeff + "x^" + exp);
            }
    }
 public int compareTo(Object other)
    {
        if(exp ==((termNode) other).exp)
            return 0;
        else if(exp < ((termNode) other).exp)
            return -1;
        else
            return 1;
    }

}

这是我的Test类来运行程序。

import java.util.Scanner;

class PolyTest{

    public static void main(String [] args)
    {
        Scanner kb = new Scanner(System.in);
        Polynomial r;
        Polynomial p = new Polynomial();
        System.out.println("Enter first polynomial.");
        p.readPolynomial(kb);
        Polynomial q = new Polynomial();
        System.out.println();
        System.out.println("Enter second polynomial.");
        q.readPolynomial(kb);
        r = Polynomial.add(p,q);
        System.out.println();
        System.out.print("The sum of ");
        p.display();
        System.out.print(" and ");
        q.display();
        System.out.print(" is ");
        r.display();
    }
}

2 个答案:

答案 0 :(得分:4)

一些建议:

  • 按惯例的类名以大写字母开头,例如TermNode
  • 使用泛型,即TermNode implements Comparable<TermNode>
  • 事实上,让Node<Term>改为
  • 可能更好

我看到的虫子:

    {li} prev = null; insert
    • 应为prev = cur;
    • 考虑将其分解为助手find - 类似方法
  • readPolynomial中,您每次迭代都会创建一个新的Polynomial,并且不会对this执行任何操作。
    • 您希望在[{1}}方法中插入字词,或在this方法结尾处Polynomial保留一个return
  • static readPolynomial
    • 你想在这里完成什么?这是一个无限循环!

更多建议:

  • 两次传递中while (first != null) sum.setNext(first);可能更容易/更易读/等等:
      首先
    • add个多项式,确保条款正确排序
      • merge3x+1合并到2x+2
    • 然后通过合并具有相同指数的任何连续的术语对来3x+2x+1+2
      • simplify简化为3x+2x
  • 一旦开始工作,请考虑在必要时一次性优化5x
  • 实际上,使用add可以在O(N^2)中轻松完成多项式合并
    • 首先纠正,然后优化到insert
  • 在调试/开发时,经常打印多项式。读完之后再做。插入后再做。在O(N)第一次通过后执行此操作。在第二次通过后再做。

答案 1 :(得分:0)

我可以立即看到的一个错误是:在遍历列表时,您需要先将当前节点cur保存到prev,然后才能继续前进。但您始终将null分配给prev

while (cur!=null && (newNode.compareTo(cur)<0)) {
    prev = null;//  <---- should be prev = cur;
    cur = cur.getNext();