Results 1 to 2 of 2
- 08-31-2011, 07:59 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 1
- Rep Power
- 0
Singly Linked List Java implementation
Ok for a Uni assignment I am being requested to construct a Polynomial Class with various methods. I get the general jist of what I need to accomplish but I am having trouble with one method.
What I am having trouble with is where I need to add a new node to the Start of a linked list, I am not able to using the code below. I only get the original value of the polynomial Returned.
Any prods in the right direction would be great!
Java Code:public static Polynomial add(Polynomial polynomial, double coefficient, int power) { //Check validity of Polynomial if (polynomial == null) return null; if (Math.abs(coefficient) < epsilon) coefficient = 0.0; if (coefficient == 0.0) return null; if (power < 0) return null; //If power is less than smallest node add as last node (works) if (power < polynomial.powerMin()) { Polynomial newNode = new Polynomial(coefficient,power); if (polynomial.successor != null) { while (polynomial.successor != null) { polynomial.successor = polynomial.successor.successor; } polynomial.successor = newNode; } else if (polynomial.successor == null ) polynomial.successor = newNode; } //if power is greater than the first node add to the front (Doesn't work) else if (power > polynomial.powerMax()) if (polynomial == null) { Polynomial newNode = new Polynomial(coefficient,power); polynomial = newNode; } else{ { Polynomial newNode = new Polynomial(coefficient,power); newNode.successor = polynomial; polynomial = newNode; return polynomial; } } //where power is the same in both nodes, add coefficients and leave power alone (works) else { if (power == polynomial.power) polynomial.coefficient = polynomial.coefficient + coefficient; } return polynomial; }
- 08-31-2011, 01:36 PM #2
Similar Threads
-
Linked List implementation of various classes
By the_rider9 in forum New To JavaReplies: 2Last Post: 05-21-2011, 03:12 AM -
Linked list implementation using core java
By jhapranay in forum Advanced JavaReplies: 0Last Post: 02-03-2011, 03:15 PM -
Trouble with circular singly linked queue
By somewierdguy in forum New To JavaReplies: 2Last Post: 12-06-2010, 01:48 AM -
Implementing a singly linked list
By Onra in forum New To JavaReplies: 2Last Post: 04-12-2010, 09:19 PM -
Trouble Developing Singly Linked Circular List
By VinceGuad in forum New To JavaReplies: 14Last Post: 02-25-2009, 04:38 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks