-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorted1_Linked_list_DS.txt
121 lines (95 loc) · 1.84 KB
/
sorted1_Linked_list_DS.txt
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include<stdio.h>
#include<malloc.h>
typedef struct list
{
int data;
struct list *next;
} list;
void insert_sort(list **head,int value)
{
list *node=(list *)malloc(sizeof(list));
node->data=value;
node->next=NULL;
if(*head==NULL || (*head)->data>=value)
{
node->next=*head;
*head=node;
}
else
{
list *p=*head;
while(p->next !=NULL)
{
if(p->data<=value && (p->next)->data >=value )
{
node->next=p->next;
p->next=node;
return;
}
else
{
p=p->next;
}
}
p->next=node;
}
}
void insert_end(list **head,int value)
{
list *node=(list *)malloc(sizeof(list));
node->data=value;
node->next=NULL;
if(*head==NULL)
{
*head=node;
}
else
{
list *p=*head;
while(p->next !=NULL)
{
p=p->next;
}
p->next=node;
}
}
void print(list *head)
{
if(head==NULL)
{
printf("List is empty");
}
else
{
while(head != NULL)
{
printf("%d -> ",head->data);
head=head->next;
}
printf("NULL\n");
}
}
int main()
{
list *head1=NULL;
list *head2=NULL;
int i,n,value;
printf("Enter total size : ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
scanf("%d",&value);
insert_end(&head1,value);
}
printf("List is : ");
print(head1);
list *p=head1;
while(p !=NULL)
{
insert_sort(&head2,p->data);
p=p->next;
}
printf("Sorted Linked List is : ");
print(head2);
return 0;
}