Skip to content

Commit 9f10a4f

Browse files
Merging sorted linked lists in C
1 parent 15ce0b4 commit 9f10a4f

File tree

2 files changed

+298
-0
lines changed

2 files changed

+298
-0
lines changed

linked-list/merging-sorted-lists.c

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
#include<stdio.h>
7+
#include<stdlib.h>
8+
9+
struct node
10+
{
11+
int info;
12+
struct node *link;
13+
};
14+
15+
struct node *createSortedList(struct node *start);
16+
struct node *insertInOrder(struct node *start,int data);
17+
void displayList(struct node *start);
18+
struct node *merge(struct node *p1,struct node *p2);
19+
20+
main()
21+
{
22+
struct node *start1=NULL,*start2=NULL, *startM;
23+
24+
start1=createSortedList(start1);
25+
start2=createSortedList(start2);
26+
27+
printf("List1 : ");
28+
displayList(start1);
29+
30+
printf("List2 : ");
31+
displayList(start2);
32+
33+
startM = merge(start1, start2);
34+
displayList(startM);
35+
36+
}/*End of main()*/
37+
38+
struct node *merge(struct node *p1,struct node *p2)
39+
{
40+
struct node *startM, *temp, *pM;
41+
42+
temp=(struct node *)malloc(sizeof(struct node));
43+
temp->link=NULL;
44+
startM=temp;
45+
46+
if(p1->info <= p2->info)
47+
{
48+
temp->info=p1->info;
49+
p1=p1->link;
50+
}
51+
else
52+
{
53+
temp->info=p2->info;
54+
p2=p2->link;
55+
}
56+
57+
pM=startM;
58+
while(p1!=NULL && p2!=NULL)
59+
{
60+
temp=(struct node *)malloc(sizeof(struct node));
61+
if(p1->info <= p2->info)
62+
{
63+
temp->info=p1->info;
64+
p1=p1->link;
65+
}
66+
else
67+
{
68+
temp->info=p2->info;
69+
p2=p2->link;
70+
}
71+
temp->link=NULL;
72+
pM->link=temp;
73+
pM=temp;
74+
}
75+
76+
/*second list has finished and elements left in first list*/
77+
while(p1!=NULL)
78+
{
79+
temp=(struct node *)malloc(sizeof(struct node));
80+
temp->info=p1->info;
81+
temp->link=NULL;
82+
pM->link=temp;
83+
pM=temp;
84+
85+
p1=p1->link;
86+
}
87+
88+
/*If first list has finished and elements left in second list*/
89+
while(p2!=NULL)
90+
{
91+
temp=(struct node *)malloc(sizeof(struct node));
92+
temp->info=p2->info;
93+
temp->link=NULL;
94+
pM->link=temp;
95+
pM=temp;
96+
97+
p2=p2->link;
98+
}
99+
100+
return startM;
101+
}
102+
103+
struct node *createSortedList(struct node *start )
104+
{
105+
int i,n,data;
106+
printf("Enter the number of nodes : ");
107+
scanf("%d",&n);
108+
start=NULL;
109+
110+
for(i=1; i<=n; i++)
111+
{
112+
printf("Enter the element to be inserted : ");
113+
scanf("%d",&data);
114+
start=insertInOrder(start, data);
115+
}
116+
return start;
117+
}/*End of createSortedList()*/
118+
119+
struct node *insertInOrder(struct node *start,int data)
120+
{
121+
struct node *p,*temp;
122+
temp=(struct node *)malloc(sizeof(struct node));
123+
temp->info=data;
124+
125+
/*List empty or new node to be inserted before first node*/
126+
if(start==NULL || data<start->info)
127+
{
128+
temp->link=start;
129+
start=temp;
130+
return start;
131+
}
132+
else
133+
{
134+
p=start;
135+
while(p->link!=NULL && p->link->info < data)
136+
p=p->link;
137+
temp->link=p->link;
138+
p->link=temp;
139+
}
140+
return start;
141+
}/*End of insertInOrder()*/
142+
143+
144+
void displayList(struct node *start)
145+
{
146+
struct node *p;
147+
if(start==NULL)
148+
{
149+
printf("List is empty\n");
150+
return;
151+
}
152+
p=start;
153+
while(p!=NULL)
154+
{
155+
printf("%d ",p->info);
156+
p=p->link;
157+
}
158+
printf("\n");
159+
}/*End of displayList()*/
160+
161+
162+
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
#include<stdio.h>
7+
#include<stdlib.h>
8+
9+
struct node
10+
{
11+
int info;
12+
struct node *link;
13+
};
14+
15+
struct node *createSortedList(struct node *start);
16+
struct node *insertInOrder(struct node *start,int data);
17+
void displayList(struct node *start);
18+
struct node *merge(struct node *p1,struct node *p2);
19+
20+
main()
21+
{
22+
struct node *start1=NULL,*start2=NULL, *startM;
23+
24+
start1=createSortedList(start1);
25+
start2=createSortedList(start2);
26+
27+
printf("List1 : ");
28+
displayList(start1);
29+
30+
printf("List2 : ");
31+
displayList(start2);
32+
33+
startM = merge(start1, start2);
34+
displayList(startM);
35+
36+
}/*End of main()*/
37+
38+
39+
struct node *merge(struct node *p1,struct node *p2)
40+
{
41+
struct node *startM,*pM;
42+
43+
if(p1->info <= p2->info)
44+
{
45+
startM = p1;
46+
p1 = p1->link;
47+
}
48+
else
49+
{
50+
startM = p2;
51+
p2 = p2->link;
52+
}
53+
54+
pM = startM;
55+
56+
while( p1!=NULL && p2!=NULL )
57+
{
58+
if( p1->info <= p2->info )
59+
{
60+
pM->link = p1;
61+
pM = pM->link;
62+
p1 = p1->link;
63+
}
64+
else
65+
{
66+
pM->link = p2;
67+
pM = pM->link;
68+
p2 = p2->link;
69+
}
70+
}
71+
72+
if(p1==NULL)
73+
pM->link=p2;
74+
else
75+
pM->link=p1;
76+
return startM;
77+
}
78+
79+
struct node *createSortedList(struct node *start )
80+
{
81+
int i,n,data;
82+
printf("Enter the number of nodes : ");
83+
scanf("%d",&n);
84+
start=NULL;
85+
86+
for(i=1; i<=n; i++)
87+
{
88+
printf("Enter the element to be inserted : ");
89+
scanf("%d",&data);
90+
start=insertInOrder(start, data);
91+
}
92+
return start;
93+
}/*End of createSortedList()*/
94+
95+
struct node *insertInOrder(struct node *start,int data)
96+
{
97+
struct node *p,*temp;
98+
temp=(struct node *)malloc(sizeof(struct node));
99+
temp->info=data;
100+
101+
/*List empty or new node to be inserted before first node*/
102+
if(start==NULL || data<start->info)
103+
{
104+
temp->link=start;
105+
start=temp;
106+
return start;
107+
}
108+
else
109+
{
110+
p=start;
111+
while(p->link!=NULL && p->link->info < data)
112+
p=p->link;
113+
temp->link=p->link;
114+
p->link=temp;
115+
}
116+
return start;
117+
}/*End of insertInOrder()*/
118+
119+
120+
void displayList(struct node *start)
121+
{
122+
struct node *p;
123+
if(start==NULL)
124+
{
125+
printf("List is empty\n");
126+
return;
127+
}
128+
p=start;
129+
while(p!=NULL)
130+
{
131+
printf("%d ",p->info);
132+
p=p->link;
133+
}
134+
printf("\n");
135+
}/*End of displayList()*/
136+

0 commit comments

Comments
 (0)