This is for an assignment, but I'm not trying to copy and paste code, I want to know why this is happening. Part of the assignment is to output a polynomial expression, and I must do so by order of the highest degree. From my understanding, if I continuously add a head, the expression will be in reverse, so I've decided instead to add to the end of the list (the tail). I thought this way, I could print out every item while traversing the list. However, it would appear that for some reason, a reference might be lost in the list and I can only output the head instead of the rest of the list. If try to start at the tail, it will be in reverse. So unless there's some workaround to this, perhaps you guys can help me understand what I'm doing wrong, so I can get a better grasp about linked lists. By the way, I can only implement singly linked lists, not doubly, for the purposes of the assignment.
Here is my code... The main focus here are the toString, addToStart, and addToEnd methods method outside of the Term class which prints out my results. So for example, when I input 3 terms of the expressions, I only get the term in the head of the list, the other two are lost. Also, please note this code is incomplete.
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;
}
}
}
This is what I have for my addToStart() method, just to give you an idea.
/**
* 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++;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments