-
Notifications
You must be signed in to change notification settings - Fork 0
/
merging_linked_list.c
96 lines (78 loc) · 1.93 KB
/
merging_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
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node *next;
};
void lltrav(struct Node *head){
struct Node *ptr=head;
while(ptr!=NULL){
printf("%d ",ptr->data);
ptr=ptr->next;
}
printf("\n");
}
void llelement(struct Node *one,int d,struct Node *two){
one->data=d;
one->next=two;
}
struct Node *merging(struct Node *head1,struct Node *head2){
struct Node *ptr=(struct Node *)malloc(sizeof(struct Node));
struct Node *p1=head1;
struct Node *p2=head2;
struct Node *p3=ptr;
while(p1!=NULL && p2!=NULL){
if(p1->data < p2->data){
p3->next=p1;
p1=p1->next;
p3=p3->next;
}else{
p3->next=p2;
p2=p2->next;
p3=p3->next;
}
}
while(p1!=NULL){
p3->next=p1;
p1=p1->next;
p3=p3->next;
}
while(p2!=NULL){
p3->next=p2;
p2=p2->next;
p3=p3->next;
}
return ptr->next;
}
int main()
{
struct Node *head;
struct Node *sec;
struct Node *thi;
struct Node *fou;
struct Node *head2;
struct Node *sec2;
struct Node *thi2;
struct Node *fou2;
head=(struct Node*)malloc(sizeof(struct Node));
sec=(struct Node*)malloc(sizeof(struct Node));
thi=(struct Node*)malloc(sizeof(struct Node));
fou=(struct Node*)malloc(sizeof(struct Node));
head2=(struct Node*)malloc(sizeof(struct Node));
sec2=(struct Node*)malloc(sizeof(struct Node));
thi2=(struct Node*)malloc(sizeof(struct Node));
fou2=(struct Node*)malloc(sizeof(struct Node));
llelement(head,1,sec);
llelement(sec,23,thi);
llelement(thi,34,fou);
llelement(fou,67,NULL);
llelement(head2,2,sec2);
llelement(sec2,16,thi2);
llelement(thi2,21,fou2);
llelement(fou2,41,NULL);
lltrav(head);
lltrav(head2);
struct Node *head3=merging(head,head2);
lltrav(head3);
return 0;
}