We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,613 Members — Technology Publication meets Social Media

trying to build linked list that sort it selft. but this code below doesnt work. this part of code should insert a node in order. for ex.
if i have a linked list of age 1 3 4 5. and if i insert(3) than result should be 1 2 3 3 4 5. but my code output 1 2 3 4 5 3 :(

``````cur_node = head;
while(cur_node != NULL && cur_node->next != NULL)
{
if(cur_node->age < ag && cur_node->next->age > ag)
{
new_node = (struct node *) malloc(sizeof(struct node));
if(new_node == NULL)
{
printf("Node allocation failed.\n"); fflush(stdout);
}
strcpy(new_node->name, na);
new_node->age = ag;
new_node->next = cur_node->next;
cur_node->next = new_node;
in_list = 1;
}
cur_node = cur_node->next;
}
``````

i think the problem is in this statement.

``````if(cur_node->age < ag && cur_node->next->age > ag)
``````

if i have linked list of age1, 6, 7. and if i insert(2). it works fine. because 2 < 1 && 2 is > 6.
problem is if i have age1 2 4 5. and if i insert(2). it does not work bc 2 < 1 && 2 > 2. error here. can idea how can i fix the if statement.

``````if(cur_node->age < ag && cur_node->next->age >= ag)
if(cur_node->age <= ag && cur_node->next->age >= ag)
``````

they both dont work :(

/////////// full code of my insert method

``````void insert(char *na, int ag)
{
struct node *new_node;
int skip = 0;
int in_list = 0;
if(cur_node == NULL)
{
head = (struct node *) malloc(sizeof(struct node));
{
printf("Node allocation failed.\n"); fflush(stdout);
exit(1);
}
}
else
{
/* skip double */
while(cur_node != NULL)
{
if(strcmp(cur_node->name, na) == 0)
{
skip = 1;
}
cur_node = cur_node->next;
}
if(skip == 0)
{
/* insert at begining */
{
new_node = (struct node *) malloc(sizeof(struct node));
if(new_node == NULL)
{
printf("Node allocation failed.\n"); fflush(stdout);
}
strcpy(new_node->name, na);
new_node->age = ag;
in_list = 1;
}
/******************* 1 2 3 4 insert(2) ------------------ doesnt work **************/
/* insert in middle */
while(cur_node != NULL && cur_node->next != NULL)
{
if(cur_node->age < ag && cur_node->next->age > ag)
{
new_node = (struct node *) malloc(sizeof(struct node));
if(new_node == NULL)
{
printf("Node allocation failed.\n"); fflush(stdout);
}
strcpy(new_node->name, na);
new_node->age = ag;
new_node->next = cur_node->next;
cur_node->next = new_node;
in_list = 1;
}
cur_node = cur_node->next;
}
/* insert at end */
if(in_list == 0)
{
while(cur_node->next != NULL)
{
cur_node = cur_node->next;
}
new_node = (struct node *) malloc(sizeof(struct node));
if(new_node == NULL)
{
printf("Node allocation failed.\n"); fflush(stdout);
exit(1);
}
strcpy(new_node->name,na);
new_node->age = ag;
new_node->next = NULL;
cur_node->next = new_node;
}
}
}
}//end of insert method
``````
3
Contributors
1
10 Hours
Discussion Span
1 Year Ago
Last Updated
2
Views
hwoarang69
Posting Pro
569 posts since Feb 2012
Reputation Points: 6
Skill Endorsements: 7

Try this: Instead of checking both the current node and the next node, just look at the current node. If the element you're considering is less than the current node, insert it before the current node. If it's greater or equal, move on to the next node. If you hit the end of the list without finding a place for it, just put it at the end.

Is it clear why that should work?

gusano79
Practically a Master Poster
666 posts since May 2004
Reputation Points: 193