Solved

Posted on 2003-11-13
244 Views
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;
}

}
}
0
Question by:Rebecca2003
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

Accepted Solution

sbooth17 earned 30 total points
ID: 9740825
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;
}
}
0

LVL 39

Expert Comment

ID: 9741164
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
0

LVL 11

Expert Comment

ID: 9741303
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
0

LVL 9

Expert Comment

ID: 10249141
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}

Tinchos
EE Cleanup Volunteer
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
###### Suggested Courses
Course of the Month3 days, 10 hours left to enroll