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
``````

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.