I wanted to insert a value to a linked list in ascending order. But when i insert 5,10,2 - it will print out 5,2,10. When i try 4,3,8 - its correct! Pls correct my mistakes below.

{
nodePtr newNode = new node;
newNode->num = val;
newNode->next = NULL;

{
{
}
}
else
{
nodePtr temp;

while(search != NULL)
{
if(search->next == NULL)
{
search->next = newNode;
break;
}

if(search->next->num > val)
{
temp = search->next;
search->next = newNode;
newNode->next = temp;
break;
}

search = search->next;
}

}
}
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Try this, untested code, I think it should work.

nodePtr newNode = new node;
newNode->num = val;
newNode->next = NULL;

else
{
nodePtr temp;

//check the first value
if(val < search->num)
{
newNode->next = search;
return;
}

while(true)
{
if(search->next == NULL)
{
search->next = newNode;
break;
}
if(val < search->next->num)
{
temp = search;
search->next = newNode;
newNode->next = temp;
break;
}
search = search->next;
}
}

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
What type is val and num? If that are strings then "10" is less "2" because comparision works lexicographically.
Use type int if i am right.

Regards, Alex
Commented:
Rebecca2003:

You should probably keep one question in one posting rather than here and in http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20796480.html

You should go to Community Support and have one of them removed.

-bcl
Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: sbooth17 {http:#9740825}