# Adding numbers to linked list.

Page 1 of 1

## 1 Replies - 2711 Views - Last Post: 30 November 2012 - 08:38 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=302218&amp;s=c9b6c1072453533f59752475ca157dd5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 joseph2324

• New D.I.C Head

Reputation: 0
• 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

• Games, Graphs, and Auctions

Reputation: 12278
• Posts: 45,364
• 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

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }