How do I traverse through a linked list after adding tails rather than heads

Dude420

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;
    }


}
}
APengue

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to iterate through Linked List

From Dev

How do I reverse through a linked list

From Dev

How do I open nerdtree in separate window rather than split?

From Dev

How do I scroll within HTML rather than in the View?

From Dev

How flipping a coin and display tails and heads counting?

From Dev

Make an entire div linked after hover rather than only the icon

From Dev

How to display the traverse of a linked list (doubly linked list)?

From Dev

How do I create a clone of the following array rather than a reference?

From Dev

How do I traverse through model relationships in laravel with dot syntax

From Dev

NLTK: How do I traverse a noun phrase to return list of strings?

From Dev

How do I add to a UINavigationController push animation, rather than replace it?

From Dev

How do I get a normal url from redis rather than through url cPikle converted?

From Dev

How do I get the user to input an int rather than a float?

From Dev

How do I iterate through combinations of a list

From Dev

How do we traverse from nth to last element in a linked list?

From Dev

How do I use this Linked List implementation?

From Dev

How to iterate through Linked List

From Dev

How can I write to a file but keep \n as it is rather than adding a new line in python?

From Dev

How do I access an object response (rather than a list) in jqueryui autocomplete?

From Dev

How do I scroll within HTML rather than in the View?

From Dev

Copy constructor traverse through linked list. implementation

From Dev

How do I less a filename rather than an inode number?

From Dev

How do I search through a Java Linked List

From Dev

How do I rewrite my method to return a list or Movie rather than a list of String?

From Dev

Haskell - How to traverse through a list and reverse elements

From Dev

How do I Sort the names in a Linked List?

From Dev

How do I get python to search text for one word in a list rather than all the words in a list?

From Dev

How do I create a mutation that pushes to an array rather than replacing it?

From Dev

Can't traverse through a C linked list

Related Related

  1. 1

    How to iterate through Linked List

  2. 2

    How do I reverse through a linked list

  3. 3

    How do I open nerdtree in separate window rather than split?

  4. 4

    How do I scroll within HTML rather than in the View?

  5. 5

    How flipping a coin and display tails and heads counting?

  6. 6

    Make an entire div linked after hover rather than only the icon

  7. 7

    How to display the traverse of a linked list (doubly linked list)?

  8. 8

    How do I create a clone of the following array rather than a reference?

  9. 9

    How do I traverse through model relationships in laravel with dot syntax

  10. 10

    NLTK: How do I traverse a noun phrase to return list of strings?

  11. 11

    How do I add to a UINavigationController push animation, rather than replace it?

  12. 12

    How do I get a normal url from redis rather than through url cPikle converted?

  13. 13

    How do I get the user to input an int rather than a float?

  14. 14

    How do I iterate through combinations of a list

  15. 15

    How do we traverse from nth to last element in a linked list?

  16. 16

    How do I use this Linked List implementation?

  17. 17

    How to iterate through Linked List

  18. 18

    How can I write to a file but keep \n as it is rather than adding a new line in python?

  19. 19

    How do I access an object response (rather than a list) in jqueryui autocomplete?

  20. 20

    How do I scroll within HTML rather than in the View?

  21. 21

    Copy constructor traverse through linked list. implementation

  22. 22

    How do I less a filename rather than an inode number?

  23. 23

    How do I search through a Java Linked List

  24. 24

    How do I rewrite my method to return a list or Movie rather than a list of String?

  25. 25

    Haskell - How to traverse through a list and reverse elements

  26. 26

    How do I Sort the names in a Linked List?

  27. 27

    How do I get python to search text for one word in a list rather than all the words in a list?

  28. 28

    How do I create a mutation that pushes to an array rather than replacing it?

  29. 29

    Can't traverse through a C linked list

HotTag

Archive