Results 1 to 2 of 2
  1. #1
    xayto is offline Member
    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0

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

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    I need to add a new node to the Start of a linked list,
    When adding to the beginning of a linked list, how do you set the list's pointer to include the new node?

    I only get the original value of the polynomial Returned.
    What is your method supposed to return?

Similar Threads

  1. Linked List implementation of various classes
    By the_rider9 in forum New To Java
    Replies: 2
    Last Post: 05-21-2011, 04:12 AM
  2. Linked list implementation using core java
    By jhapranay in forum Advanced Java
    Replies: 0
    Last Post: 02-03-2011, 04:15 PM
  3. Trouble with circular singly linked queue
    By somewierdguy in forum New To Java
    Replies: 2
    Last Post: 12-06-2010, 02:48 AM
  4. Implementing a singly linked list
    By Onra in forum New To Java
    Replies: 2
    Last Post: 04-12-2010, 10:19 PM
  5. Trouble Developing Singly Linked Circular List
    By VinceGuad in forum New To Java
    Replies: 14
    Last Post: 02-25-2009, 05:38 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
  •