【发布时间】: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