添加尾部而不是头部后如何遍历链表
How do I traverse through a linked list after adding tails rather than heads
这是作业,但我不是要复制和粘贴代码,我想知道为什么会这样。作业的一部分是输出一个多项式表达式,我必须按照最高次数的顺序进行。根据我的理解,如果我连续添加一个头,表达式将相反,所以我决定改为添加到列表的末尾(尾部)。我想这样,我可以在遍历列表时打印出每一项。但是,由于某种原因,列表中可能会丢失引用,我只能输出头部而不是列表的其余部分。如果尝试从尾部开始,它会反过来。所以除非有一些解决方法,也许你们可以帮助我理解我做错了什么,这样我就可以更好地掌握链表。顺便说一下,为了赋值,我只能实现单链表,不能实现双链表。
这是我的代码...这里主要关注的是打印出我的结果的 Term class 之外的 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;
}
}
}
您应该尝试用图表表示方框和箭头的情况。看起来问题是当您添加到列表的尾部时。您需要更新旧尾巴,使其指向新尾巴而不是被遗忘。
这就是我的 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++;
}
这是作业,但我不是要复制和粘贴代码,我想知道为什么会这样。作业的一部分是输出一个多项式表达式,我必须按照最高次数的顺序进行。根据我的理解,如果我连续添加一个头,表达式将相反,所以我决定改为添加到列表的末尾(尾部)。我想这样,我可以在遍历列表时打印出每一项。但是,由于某种原因,列表中可能会丢失引用,我只能输出头部而不是列表的其余部分。如果尝试从尾部开始,它会反过来。所以除非有一些解决方法,也许你们可以帮助我理解我做错了什么,这样我就可以更好地掌握链表。顺便说一下,为了赋值,我只能实现单链表,不能实现双链表。
这是我的代码...这里主要关注的是打印出我的结果的 Term class 之外的 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;
}
}
}
您应该尝试用图表表示方框和箭头的情况。看起来问题是当您添加到列表的尾部时。您需要更新旧尾巴,使其指向新尾巴而不是被遗忘。
这就是我的 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++;
}