Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-10-2005, 02:13 AM   PM User | #1
squirellplaying
Regular Coder

 
Join Date: Jan 2004
Location: Maryland
Posts: 468
Thanks: 0
Thanked 0 Times in 0 Posts
squirellplaying is an unknown quantity at this point
LinkedLists (Java)

I can not seem to get my head around Linked Lists! For an assignment we have to insert numbers into a LinkedList in order. But my method seems to only insert one record! When I was just adding the new value to the end of the list all the values were added so it must be in this method. Basicly each new value gets set as the first Node, even if it shouldn't be.
Code:
  public void insertNew(Object value){
  	ListNode next;
  	
  	if(first == null){ //Check to see if there is atleast one Node already
  		next = new ListNode(value, null);
  		first = next;
  		last = first;
  	}
  	else{
  	
  	ListNode temp = first; //Start at the beginning of the list.
  	ListNode back = null;
  	int myTemp = (Integer)temp.getValue();
  	int myNext = (Integer)value;	
  		
  		while(temp.getNext()!= null && myTemp < myNext){
  			
  			System.out.println(myTemp + "<Temp  Next>" + myNext);
  			back = temp;
  			myTemp = (Integer)temp.getValue();
  			temp = temp.getNext();
  			
  		}
  		
  		if(temp == null)
  		next = new ListNode(value, null);
  		
  		else
  		next = new ListNode(value, temp.getNext());
  		
  		if(back != null)
  		back.setNext(next);	
  	}
  	
  }
I believe my basic algorithm is:
  • Check to see if first Node is set.
  • Loop through list comparing the value to the value I'm trying to insert.
  • When the current value is greater then the value I'm inserting, but the new value infront of the current value. Make the value behind it(back) link to this new value, and the new value link to the current value.

Is this correct?

Last edited by squirellplaying; 04-10-2005 at 04:29 AM..
squirellplaying is offline   Reply With Quote
Old 04-12-2005, 01:26 AM   PM User | #2
squirellplaying
Regular Coder

 
Join Date: Jan 2004
Location: Maryland
Posts: 468
Thanks: 0
Thanked 0 Times in 0 Posts
squirellplaying is an unknown quantity at this point
I've edited it too :
Code:
  public void insertNew(Object value)
  {
    ListNode temp, back, next;
    int difference;
    
    
    if(first == null || ((Integer)first.getValue() - (Integer)value) <= 0) //check the first value
    {
		addFirst(value);
	}
    else if( ((Integer)getLast() - (Integer)value) > 0 && getLast()!=null){ //check the last value
    	addLast(value);
    }
	    else { //put inbetween somewhere
	    	temp = back = first;
	    	
	    	while(((Integer)temp.getValue() - (Integer)value) >= 0 && getLast()!=null){
	    		back = temp;
	    		temp = temp.getNext();
	    	}
	    	next = new ListNode(value, temp);
	    	back.setNext(next);
	    }
 
  }
Now I get stuff about the addLast method. It should accept an object, which it does, so I don't understand.
squirellplaying is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:58 PM.


Advertisement
Log in to turn off these ads.