Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 04-10-2005, 03:13 AM PM User | #1 squirellplaying Regular Coder   Join Date: Jan 2004 Location: Maryland Posts: 468 Thanks: 0 Thanked 0 Times in 0 Posts 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 + "" + 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 05:29 AM..
 04-12-2005, 02:26 AM PM User | #2 squirellplaying Regular Coder   Join Date: Jan 2004 Location: Maryland Posts: 468 Thanks: 0 Thanked 0 Times in 0 Posts 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.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 01:23 PM.