-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmerging-sorted-lists.c
162 lines (135 loc) · 2.72 KB
/
merging-sorted-lists.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
/*
Copyright (C) Deepali Srivastava - All Rights Reserved
This code is part of DSA course available on CourseGalaxy.com
*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
struct node *createSortedList(struct node *start);
struct node *insertInOrder(struct node *start,int data);
void displayList(struct node *start);
struct node *merge(struct node *p1,struct node *p2);
main()
{
struct node *start1=NULL,*start2=NULL, *startM;
start1=createSortedList(start1);
start2=createSortedList(start2);
printf("List1 : ");
displayList(start1);
printf("List2 : ");
displayList(start2);
startM = merge(start1, start2);
displayList(startM);
}/*End of main()*/
struct node *merge(struct node *p1,struct node *p2)
{
struct node *startM, *temp, *pM;
temp=(struct node *)malloc(sizeof(struct node));
temp->link=NULL;
startM=temp;
if(p1->info <= p2->info)
{
temp->info=p1->info;
p1=p1->link;
}
else
{
temp->info=p2->info;
p2=p2->link;
}
pM=startM;
while(p1!=NULL && p2!=NULL)
{
temp=(struct node *)malloc(sizeof(struct node));
if(p1->info <= p2->info)
{
temp->info=p1->info;
p1=p1->link;
}
else
{
temp->info=p2->info;
p2=p2->link;
}
temp->link=NULL;
pM->link=temp;
pM=temp;
}
/*second list has finished and elements left in first list*/
while(p1!=NULL)
{
temp=(struct node *)malloc(sizeof(struct node));
temp->info=p1->info;
temp->link=NULL;
pM->link=temp;
pM=temp;
p1=p1->link;
}
/*If first list has finished and elements left in second list*/
while(p2!=NULL)
{
temp=(struct node *)malloc(sizeof(struct node));
temp->info=p2->info;
temp->link=NULL;
pM->link=temp;
pM=temp;
p2=p2->link;
}
return startM;
}
struct node *createSortedList(struct node *start )
{
int i,n,data;
printf("Enter the number of nodes : ");
scanf("%d",&n);
start=NULL;
for(i=1; i<=n; i++)
{
printf("Enter the element to be inserted : ");
scanf("%d",&data);
start=insertInOrder(start, data);
}
return start;
}/*End of createSortedList()*/
struct node *insertInOrder(struct node *start,int data)
{
struct node *p,*temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->info=data;
/*List empty or new node to be inserted before first node*/
if(start==NULL || data<start->info)
{
temp->link=start;
start=temp;
return start;
}
else
{
p=start;
while(p->link!=NULL && p->link->info < data)
p=p->link;
temp->link=p->link;
p->link=temp;
}
return start;
}/*End of insertInOrder()*/
void displayList(struct node *start)
{
struct node *p;
if(start==NULL)
{
printf("List is empty\n");
return;
}
p=start;
while(p!=NULL)
{
printf("%d ",p->info);
p=p->link;
}
printf("\n");
}/*End of displayList()*/