1. Member
Join Date
Jan 2011
Posts
9
Rep Power
0

## Creating a node for polynomial linked list

Does java provide a built-in way to add polynomials as linked lists?
Last edited by Java-Guy; 03-16-2011 at 09:34 PM.

2. Originally Posted by Java-Guy
Does java provide a built-in way to add polynomials as linked lists?
No it doesn't; you have to create your own List<Node> polynomials. A Node stores a coefficient and an exponent.

kind regards,

Jos

3. Member
Join Date
Jan 2011
Posts
9
Rep Power
0
Hmm, this make sense. I'm doing this for an assignment, and part of the reqs are to make an inner Term class with 2 int fields and another 2 int fields in a constructor. This is paired with an inner Node class that also has two fields (Node and Term), along with a constructor that also has Node and Term params.

I suppose I could do this:
Java Code:
public class Term {
int c, x;
Node ply;
public Term (int cf, int ex){
c=cf;
x=ex;
}
}
public class Node {
Term aTerm;
Node aNode;
public Node(Term t1, Node n1){
aTerm=t1;
aNode=n1;
}
}
But what else should I add?

4. Originally Posted by Java-Guy
Hmm, this make sense. I'm doing this for an assignment, and part of the reqs are to make an inner Term class with 2 int fields and another 2 int fields in a constructor. This is paired with an inner Node class that also has two fields (Node and Term), along with a constructor that also has Node and Term params.

I suppose I could do this:
Java Code:
public class Term {
int c, x;
Node ply;
public Term (int cf, int ex){
c=cf;
x=ex;
}
}
public class Node {
Term aTerm;
Node aNode;
public Node(Term t1, Node n1){
aTerm=t1;
aNode=n1;
}
}
But what else should I add?
I wouldn't do it that way: I'd use a TreeSet<Node> where a Node implements the Comparable<Node> interface which sorts on the exponent of the Node. That way the Nodes are automatically sorted and there can be only one Node with a certain exponent (just what you need).

kind regards,

Jos

5. Member
Join Date
Jan 2011
Posts
9
Rep Power
0
I think I understand what you're trying to do, but my prof wants me to do it with the node and term. Thanks for the suggestion though!

6. Originally Posted by Java-Guy
I think I understand what you're trying to do, but my prof wants me to do it with the node and term. Thanks for the suggestion though!
Your implementation of the Node class suggests that you even have to implement your own List? Is your professor afraid of already available (and thoroughly tested) classes?

kind regards,

Jos

7. Member
Join Date
Jan 2011
Posts
9
Rep Power
0
Yes, we have to a Node field that represents the polynomial as a singly-linked list with a list head. We tend to do things the hard way.

8. Originally Posted by Java-Guy
Yes, we have to a Node field that represents the polynomial as a singly-linked list with a list head. We tend to do things the hard way.
Well, start designing (the most advanced tools you need are paper and pencil); b.t.w. the classes you showed aren't nested classes, i.e. you have to write a class 'around' it that implements the list that makes up the polynomial.

kind regards,

Jos

9. Member
Join Date
Jan 2011
Posts
9
Rep Power
0
Yes, I know, I'm writing them inside of another class, I just didn't want to clutter up the page here.
Java Code:
class Example {
class Term {...}
class Node {...}
}
The biggest problem I'm having is how to represent the polynomial as a Node w/ a list head (and on top of this, ordered by exponent small to large).:confused: I haven't had experience with this yet, so any help would be appreciated!