Results 1 to 9 of 9
  1. #1
    Java-Guy is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Question 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 08:34 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Java-Guy View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Java-Guy is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    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. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Java-Guy View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Java-Guy is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    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. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Java-Guy View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Java-Guy is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    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. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Java-Guy View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Java-Guy is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    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!

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2011, 09:34 AM
  2. Linked List node class help!!
    By SteroidalPsycho in forum New To Java
    Replies: 0
    Last Post: 05-03-2010, 01:21 AM
  3. How to link a Array elemant to a Linked list Node
    By ravinda in forum New To Java
    Replies: 2
    Last Post: 04-18-2009, 09:16 AM
  4. Creating an Iterator for Linked List
    By spanky in forum New To Java
    Replies: 3
    Last Post: 10-05-2008, 07:16 PM
  5. creating a deck of cards using a linked list
    By boomba88 in forum New To Java
    Replies: 2
    Last Post: 09-11-2008, 03:34 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •