1 Replies - 1204 Views - Last Post: 30 November 2012 - 08:38 PM Rate Topic: -----

#1 joseph2324  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 26-April 12

Adding numbers to linked list.

Posted 30 November 2012 - 01:06 PM

Hi i am working on the problem where addElementToList() will add numbers in beginning, middle, and end. Lets say i read numbers in correct order from smallest to highest such as 2,5,60,150
First if statement adds numbers to the beginning second if statement adds numbers in the middle (those two works fine). But when i try to add numbers to the end of the list it replaces the old last number with new last number. Lets say i enter 200 the list will look like this 2,5,60,200 Then i enter 340 the list will be 2,5,60,340 instead of 2,5,60,150,200,340. So basically it does not add last number to the list it replaces it. How should i modify my while loop so the last number will be added to the list without replacing the older one?

 public void addElementToList()
      {
         Scanner scan = new Scanner(System.in);
       
         
         	double numToAdd = 0.0;
         	boolean validNumber = false;double count =0;
         
         do
         {
            System.out.println("Enter double number");
            String str = scan.nextLine();
         
            try
            {
            
            	
               numToAdd = Double.parseDouble(str);
               validNumber = true;
            	
               NumNode aNode = new NumNode(numToAdd);
               
           			
               
             
               
               previous = null;
               current = head;
               if (current.getNum() > aNode.getNum()) {
                 aNode.next = head;
                 head = aNode;
               }

               //check body
               else {
                 
                   if(current == previous&& previous.getNum()> numToAdd){
                     aNode.next = head;
                     head = aNode;
                     break;
                   }   
                   
                   if(current.getNum() > numToAdd){// this inserts in middle>
                     aNode.next = current.next;
                     current.next = aNode;
                     break;
                   }
                   
                   
                   while(aNode.getNum() > current.getNum() && current.next != null){
                	      previous = current; 
                	      current = current.next;
                	    }
                   previous.next = aNode;
                   aNode.next = current.next ;
                	  }count++;
            	
            }
            
               catch (NumberFormatException nfe) 
               {
                  System.out.println("is not a Double number");
               }   
            System.out.println("This number added to list: " + numToAdd);
         		
         } while(!validNumber && count<1);
      
      
      
      }//end addElementToList()


Is This A Good Question/Topic? 0
  • +

Replies To: Adding numbers to linked list.

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,273
  • Joined: 27-December 08

Re: Adding numbers to linked list.

Posted 30 November 2012 - 08:38 PM

A few points. First, current.next should reference the new Node. Second, separate your LinkedList logic from your user input portion. The addElementsToList() method isn't designed well. Have a single add() method that accepts a single int parameter, and add that to the List. The Scanner logic should be implemented in a separate class that manages a LinkedList object.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1