forked from rk2279709/DSA-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DSA_Ass2_10.c
184 lines (175 loc) · 3.38 KB
/
DSA_Ass2_10.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *start1=NULL;
struct node *start2=NULL;
struct node* Create_node()
{
struct node *n;
n=(struct node*)malloc(sizeof(struct node));
n->next=NULL;
return n;
}
void insert(struct node **start)
{
struct node *t,*p;
t=Create_node();
printf("Enter Data :");
scanf("%d",&t->data);
if(*start==NULL)
{
*start=t;
}
else
{
p=(*start);
while(p->next!=NULL)
{
p=p->next;
}
p->next=t;
}
}
int check_sort(struct node *head)
{
struct node *p;
p=head;
while(p->next!=NULL)
{
if(p->data>p->next->data)
{
return 0;
}
p=p->next;
}
return 1;
}
void sort(struct node **head)
{
int temp;
struct node *p,*q;
for(p=(*head);p->next!=NULL;p=p->next)
{
for(q=p->next;q->next!=NULL;q=q->next)
{
if(p->data>q->data)
{
temp=p->data;
p->data=q->data;
q->data=temp;
}
}
}
}
void merge_sort(struct node *head1,struct node *head2)
{
struct node *last,*p,*q,*head3;
p=head1;
q=head2;
if(check_sort(head1)&&check_sort(head2))
{
printf("\nList Are Not Sorted\n");
return;
}
else
{
if(p->data<q->data)
{
last=p;
head3=p;
p=p->next;
last->next=NULL;
}
else
{
last=q;
head3=q;
q=q->next;
last->next=q;
}
while(p!=NULL&& q!=NULL)
{
if(p->data<q->data)
{
last->next=p;
last=p;
p=p->next;
last->next=NULL;
}
else
{
last->next=q;
last=q;
q=q->next;
last->next=q;
}
}
if(p==NULL)
last->next=q;
else
last->next=p;
}
view_list(head3);
}
void view_list(struct node *start)
{
struct node *t;
if(start==NULL)
printf("\nList Is Empty\n");
else
{
t=start;
while(t!=NULL)
{
printf("%d\n",t->data);
t=t->next;
}
}
}
int main()
{
int choice;
printf("1.Insert_node_List1");
printf("\n2.Insert_node_list2");
printf("\n3.Sort List 1 ");
printf("\n4.Sort list 2 ");
printf("\n5.View List 1");
printf("\n6.View List 2");
printf("\n7.Merge_sort");
printf("\n8.Exit\n");
while(1)
{
printf("\nEnter Your Choice :");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert(&start1);
break;
case 2:
insert(&start2);
break;
case 3:
sort(&start1);
break;
case 4:
sort(&start2);
break;
case 5:
view_list(start1);
break;
case 6:
view_list(start2);
break;
case 7:
merge_sort(start1,start2);
break;
case 8:
exit(0);
}
}
}