【问题标题】:How do I traverse through a linked list after adding tails rather than heads添加尾部而不是头部后如何遍历链表
【发布时间】:2015-04-11 00:10:56
【问题描述】:

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

这是我的代码...这里的主要焦点是 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;
    }


}
}

【问题讨论】:

  • 这看起来像 Comp249 作业,哈哈
  • 添加到列表头部和尾部的元素永远不会连接。同样添加到尾部必须在当前尾部之后添加元素(然后用新尾部替换旧尾部),而不是在新尾部之后添加当前尾部。
  • 在开始插入时,必须先新建一个Term,并指向当前头部。一旦你这样做了,你可以说 head = theNewTerm。但是您仍然必须检查某些情况,例如头部为空时
  • @fabian,我有种感觉,谢谢。

标签: java linked-list


【解决方案1】:

这是我的 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++;
}

【讨论】:

  • 哈哈,对,你没看错,是249作业。因此,据我所知,您是否对 addToStart 方法使用 while 循环来插入术语?我想我当时对作业没有完全理解。
  • 嗯,addToStart(和addToEnd)的重点只是在多项式中添加一项。当您提示用户输入条款时,这将在 Driver 程序中完成。他们将输入一个系数和一个度数,您只需获取这些值并 addToEnd(coef, degree) 只是一个术语,然后您再次询问他们,所以是的,这部分是在一个循环中。但是在 addToStart 方法本身中,每次调用方法时,您总是只在多项式中添加一项
  • 对,我只是想确定一下,我使用这些方法的方式与您所说的完全一致。我的主要问题只是按顺序输出这些术语。可能没有足够的时间来改变一切,但我仍然会尝试在考试前更好地理解。
  • 我认为您不必按顺序输出它们,只需按输入的顺序即可。我是这么解释的。如果您在“示例输出屏幕”中查看作业表的底部,您会注意到第一句话说“输入多项式 - 从最高次数的项开始。输入系数然后次数:”这对我来说意味着我们不必须确保我们从最高程度开始输出它们。这是我的解释,所以我没有浪费时间,如果您以同样的方式解释,那么绝对不要浪费时间!
  • 是的,反正我也懒得修了,谢谢你的帮助!
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2018-01-27
    • 1970-01-01
    • 2016-09-04
    • 2020-09-22
    • 1970-01-01
    • 2019-07-17
    • 2014-05-18
    • 2020-06-23
    • 2020-02-09
    相关资源
    最近更新 更多