insert correctly into linked list
Code:
include <stdio.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
void insert_node (struct node **h, struct node **t, int v) {
struct node *temp;
if ((temp = (struct node *)malloc(sizeof(struct node))) == NULL) {
printf("Node allocation failed. \n");
exit(1); /* Stop program */
}
temp->value = v; temp->next = NULL;
if (*h == NULL) {
*h = *t = temp;
}
else {
(*t)->next = temp; *t = (*t)->next;
}
}
The code above correct inserts a node to the end of the linked list. Using this code i neeed to modify to update the list so as you insert more numbers it goes in non decreasing order. EX: i already inserted numbers into linked list. 4 5 7. I want to insert number 6 so linked list would have numbers in the following order. 4 5 6 7.
I would like to use a while loop to find the right position in the linked list to insert the value, but I am having trouble making a continuation test that works.
Any ideas would be helpful!