如何在添加尾部而不是头部之后遍历链接列表

时间:2015-04-11 00:10:56

标签: java linked-list

这是一项作业,但我没有尝试复制和粘贴代码,我想知道为什么会发生这种情况。赋值的一部分是输出多项式表达式,我必须按最高度的顺序进行输出。根据我的理解,如果我不断添加一个头,表达式将反过来,所以我决定添加到列表的末尾(尾部)。我这么想,我可以在遍历列表时打印出每个项目。但是,看起来由于某种原因,引用可能会在列表中丢失,我只能输出头而不是列表的其余部分。如果尝试从尾部开始,它将是相反的。所以,除非有一些解决方法,也许你们可以帮助我理解我做错了什么,这样我就能更好地掌握链表。顺便说一下,我只能为了作业而实施单链表,而不是双重表。

这是我的代码......这里的主要焦点是Term类之外的toString,addToStart和addToEnd方法方法,它打印出我的结果。因此,例如,当我输入表达式的3个术语时,我只在列表的头部获得该术语,其他两个丢失。此外,请注意此代码不完整。

public class Polynomial {

private Term head;
private Term tail;

public Polynomial () {
    head = null;
    tail = null;
}

public Polynomial (Polynomial poly) {
    copyOfPolynomial();

}

public Polynomial copyOfPolynomial() {

    Polynomial poly = null;
    return poly;

}

public void addToStart(double coef, int deg) {                  //Step 1: User inputs values at the start of the list
    head = new Term(coef, deg, head);
}

public void addToEnd(double coef, int deg) {                    //Step 2: Method is used if user wishes to add more terms
    tail = new Term(coef, deg, tail);
}

public int numberOfTerms() {                                    //Method is used to determine number of terms in polynomial

    int count = 0;
    Term position = head;
    count++;
    position = tail;
    while(position != null) {
        count++;
        position = position.link;
    }
    return count;
}

//public Polynomial add(Polynomial poly) {                      //The sum of the original polynomial with its derivative

//}

public double evaluate(double x) {                              //This method calculates the result when an input is entered for x

    return 0.0;
}

//public Polynomial derivative() {                              //This method does the derivative of poly1

//}

//public Polynomial integral() {                                //This method does the integration of the derivative (poly2)

//}

public void readPolynomial() {                                  //This method prompts the user input a value for x

}

public String toString() {                                      

    String strPoly = "";
    Term position = head;
    while(position != null) {
        strPoly += position.toString();
        position = position.link;
    }

    return "The polynomial you have just entered is " + strPoly;
}

public boolean equals(Polynomial poly) {

    return true;
}

/*
 * Class Term starts here
 */

private class Term {

    private double coefficient;
    private int degree;
    private Term link;

    public Term() {
        coefficient = 0.0;
        degree = 0;
        link = null;
    }

    public Term(double coef, int deg, Term linkValue) {
        coefficient = coef;
        degree = deg;
        link = linkValue;
    }

    public boolean setData(double coef, int deg) {

        coefficient = coef;
        degree = deg;
        return true;
    }

    public boolean setLink(Term newLink) {

        link = newLink;
        return true;
    }

    public double getCoefficient() {

        return coefficient;
    }

    public int getDegree() {

        return degree;
    }

    public Term getLink() {

        return link;
    }

    public String toString() {                                          //Step 3: Program outputs polynomial string depending on how inputs are applied

        String strCoef = String.valueOf(coefficient);
        String strDeg = String.valueOf(degree);
        if(degree == 0) 
            return strCoef;
        else
            return strCoef + "x^" + strDeg;
    }

    public boolean equals() {

        return true;
    }


}
}

2 个答案:

答案 0 :(得分:0)

你应该尝试用方框和箭头来描绘正在发生的事情。看起来问题是当你添加到列表的尾部时。你需要更新旧的尾巴,使它指向新的尾巴而不是遗忘。

答案 1 :(得分:0)

这就是我对addToStart()方法的看法,只是为了给你一个想法。

/**
 * Validates a term before it is added to a Polynomial.
 * @param coef must not be 0
 * @param deg must be nonnegative
 * @param term term to return
 * @return term if valid coefficient and degree, null otherwise
 */
private Term validatedTerm(double coef, int deg, Term term) {
    Term temp = new Term();
    if(!temp.setData(coef, deg) || !temp.setLink(term))
        return null;
    return temp;
}

/**
 * Adds a term to the start of the Polynomial. Uses Term validatedTerm(double, int, Term)<br />
 * Will not add term if not validated.
 * @param coef coefficient of term
 * @param deg degree of term
 */
public void addToStart(double coef, int deg) {
    Term term;
    if((term = validatedTerm(coef, deg, null)) == null)
        return;

    if(head != null)
        term.setLink(head);     
    else
        tail = term;

    head = term;
    numTerms++;
}