-
Notifications
You must be signed in to change notification settings - Fork 0
/
In_Sorted_New_node_Linked List.c
96 lines (94 loc) · 1.55 KB
/
In_Sorted_New_node_Linked List.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Implement algorithm of insert a newnode in sorted Link List //
// Data Structure and Algorithm
//CODE:~
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void insert_sorted(int);
void display();
struct node
{
int info;
struct node *link;
}*first;
void main()
{
int item,choice,pos;
do
{
printf("\n(1)Insert \(2)Display \n(3)Exit \n\nEnter Choice :");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter value for Insert :");
scanf("%d",&item);
insert_sorted(item);
break;
case 2:
display();
break;
}
}while(choice < 3);
}
void insert_sorted(int item)
{
struct node *newnode,*temp,*next;
int flag = 0;
newnode = (struct node*)malloc(sizeof(struct node));
newnode->info = item;
if(first==NULL)
{
newnode->link = NULL;
first = newnode;
}
else
{
if(item<first->info)
{
newnode->link = first;
first = newnode;
}
else
{
temp = first;
while(temp->link!=NULL)
{
next = temp->link;
if(item>temp->info && item<=next->info)
{
newnode->link=next;
temp->link=newnode;
flag=1;
}
temp = temp->link;
}
if(flag==0)
{
temp->link=newnode;
newnode->link=NULL;
}
}
}
return;
}
void display()
{
struct node * temp;
if(first==NULL)
{
printf("\nLink list is Empty");
}
else
{
temp = first;
printf("\n\Link List :");
while(temp!=NULL)
{
printf("\t%d",temp->info);
temp=temp->link;
}
}
return;
}
//OUTPUT:~