#include <stdio.h>
#include <stdlib.h>
struct
Node {
int
data;
struct
Node* next;
};
void
sortedInsert(
struct
Node** head_ref,
struct
Node* new_node)
{
struct
Node* current;
if
(*head_ref == NULL
|| (*head_ref)->data
>= new_node->data) {
new_node->next = *head_ref;
*head_ref = new_node;
}
else
{
current = *head_ref;
while
(current->next != NULL
&& current->next->data < new_node->data) {
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
}
struct
Node* newNode(
int
new_data)
{
struct
Node* new_node
= (
struct
Node*)
malloc
(
sizeof
(
struct
Node));
new_node->data = new_data;
new_node->next = NULL;
return
new_node;
}
void
printList(
struct
Node* head)
{
struct
Node* temp = head;
while
(temp != NULL) {
printf
(
"%d "
, temp->data);
temp = temp->next;
}
}
int
main()
{
struct
Node* head = NULL;
struct
Node* new_node = newNode(5);
sortedInsert(&head, new_node);
new_node = newNode(10);
sortedInsert(&head, new_node);
new_node = newNode(7);
sortedInsert(&head, new_node);
new_node = newNode(3);
sortedInsert(&head, new_node);
new_node = newNode(1);
sortedInsert(&head, new_node);
new_node = newNode(9);
sortedInsert(&head, new_node);
printf
(
"\n Created Linked List\n"
);
printList(head);
return
0;
}