多项式链接列表不返回列表

时间:2014-06-24 01:09:48

标签: java

这是我第一次在这里问。我正在使用java中的链接列表创建一个多项式。

我试图将一串多项式转换为链表中的节点并进行加法和减法。

我创建了一个方法,将多项式拆分为术语并将其存储在链表中。我试图打印出该方法中的列表,并且它可以工作。但是,当我想使用toString方法打印列表或调用add或subtract方法时,它表示我的列表为null。有人可以帮助我,并向我解释为什么列表变为null?

这是我的Term类

public class Term 
{
   int coef;
   int exp;
   Term next;

public Term()
{
    coef = 0;
    exp = 0;
    next = null;
}

public Term(int coef, int exp)
{
    this.coef = coef;
    this.exp = exp;
    this.next = null;
}

public void setCoef(int coef)
{
    this.coef = coef;
}

public void setExp(int exp)
{
    this.exp = exp;
}

public int getCoef()
{
    return coef;
}

public int getExp()
{
    return exp;
}

public void setNext(Term next)
{
    this.next = next;
}

public Term getNext()
{
    return next;
}

public String toString() 
{
    String str = "";

    if (exp == 0)
    {
        if (coef < 0)
            str += coef;
        else
            str += "+" + coef;
    }
    else if (exp == 1)
    {
        if (coef < 0)
            str += coef + "x";
        else
            str += "+" + coef + "x";
    }
    else if (coef < 0)
        str += coef + "x^" + exp;
    else
        str += "+" + coef + "x^" + exp;

    return str;
}
}

我的LinkedListPolynomial类

public class LinkedListPolynomial implements PolynomialInterface 
{
int coefficient, exponent;
private Term head;

public LinkedListPolynomial() 
{
    head = null;
}

public LinkedListPolynomial(int coefficient, int exponent) 
{
    this.coefficient = coefficient;
    this.exponent = exponent;
    head = new Term(coefficient, exponent);
}

public LinkedListPolynomial(String n) 
{
    String s1New = n.replaceAll("-", "+-");
    String[] arr = s1New.split("\\+");

            //if the first term contains negative coefficient, the first term is empty
    if (arr[0].isEmpty()) {
        for (int i = 1; i < arr.length; i++)
        {
            if (arr[i].contains("x^")) 
            {
                String str = arr[i].substring(0, arr[i].indexOf("x^"));
                String poly = arr[i].substring(arr[i].indexOf("x^") + 2);

                coefficient = Integer.parseInt(str);
                exponent = Integer.parseInt(poly);

                if (head == null) 
                {
                    head = new Term(coefficient, exponent);
                } 
                else 
                {
                    Term newNode = new Term(coefficient, exponent);
                    newNode.setNext(head);
                    head = newNode;
                }
            } 
            else 
            {
                coefficient = Integer.parseInt(arr[i]);
                exponent = 0;

                if (head == null) 
                {
                    head = new Term(coefficient, exponent);
                } 
                else 
                {
                    Term newNode = new Term(coefficient, exponent);
                    newNode.setNext(head);
                    head = newNode;
                }
            }
        }
    } 
    else 
    {
        for (int i = 0; i < arr.length; i++) 
        {
            if (arr[i].contains("x^"))
            {
                String str = arr[i].substring(0, arr[i].indexOf("x^"));
                String poly = arr[i].substring(arr[i].indexOf("x^") + 2);

                coefficient = Integer.parseInt(str);
                exponent = Integer.parseInt(poly);

                if (head == null) 
                {
                    head = new Term(coefficient, exponent);
                } 
                else 
                {
                    Term newNode = new Term(coefficient, exponent);
                    newNode.setNext(head);
                    head = newNode;
                }
            } 
            else 
            {
                coefficient = Integer.parseInt(arr[i]);
                exponent = 0;

                if (head == null) 
                {
                    head = new Term(coefficient, exponent);
                } 
                else 
                {
                    Term newNode = new Term(coefficient, exponent);
                    newNode.setNext(head);
                    head = newNode;
                }
            }

        }
    }

    selectionSort();

    int i = 0;
    while (head != null) 
    {
        System.out.print(head);
        head = head.getNext();
        i++;
    }
    System.out.println("\n");

}

public PolynomialInterface add(PolynomialInterface other)
{
    LinkedListPolynomial sum = new LinkedListPolynomial();
    LinkedListPolynomial parameter = (LinkedListPolynomial) other;

    return sum;
}

public PolynomialInterface subtract(PolynomialInterface other)
{
    LinkedListPolynomial subtract = new LinkedListPolynomial();
    LinkedListPolynomial parameter = (LinkedListPolynomial) other;

    return subtract;
}

public String toString()
{
    String str = "";
    Term current = head;

    if (current == null)
        str += "it's null";
    else
    {
        while (current.getNext() != null) { current = current.getNext(); 
        str += current.getCoef() + "x^" + current.getExp(); }
    }

    return str;

}

public void selectionSort() 
{
    for (Term node1 = head; node1 != null; node1 = node1.getNext()) // number of iterations
    {
        Term min = node1;// assumes min node is the node under
                            // considerations
        // selects the min node
        for (Term node2 = node1; node2 != null; node2 = node2.getNext()) 
        {
            if (min.getExp() < node2.getExp()) 
            {
                min = node2;
            }

        }
        // swaps the min node with the node in its actual position
        Term temp = new Term(node1.getCoef(), node1.getExp());
        node1.coef = min.getCoef();
        node1.exp = min.getExp();
        min.coef = temp.getCoef();
        min.exp = temp.getExp();
    }
}
}

My PolynomialInterface

public interface PolynomialInterface 
{
PolynomialInterface add(PolynomialInterface other);
PolynomialInterface subtract(PolynomialInterface other);
String toString();
}

1 个答案:

答案 0 :(得分:1)

addsubtract方法中,您使用默认构造函数创建列表,默认构造函数将head设置为null,不执行任何操作,然后返回空名单。您需要实际实现addsubtract的功能。请参阅How do I implement a Linked List in Java?获得一份不错的入门书。

相关问题