-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge two different linked list.c
111 lines (77 loc) · 1.95 KB
/
merge two different 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
97
98
99
100
101
102
103
104
105
106
107
108
109
#include<stdio.h>
#include<stdlib.h>
///////////////// The Application is merging two different default linked list in third linked list ///////////////
struct node {
int data;
struct node *next;
};
//////////////////// adding node function /////////////////////////////
void insert_node(struct node **head,int no){
struct node *p,*r;
if(*head==NULL){
p=(struct node *)malloc(sizeof(struct node));
p->data=no;
p->next=NULL;
*head=p;
}
else{
p=*head;
while(p->next!=NULL)
p=p->next;
p->next=(struct node *)malloc(sizeof(struct node));
p=p->next;
p->data=no;
p->next=NULL;
}
}
///////////////// printing linked list function ////////////////////////////////
void print(struct node *head)
{
struct node *p;
p=head;
while(p!=NULL){
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
/////////////// merge function /////////////////////
void merge(struct node **ptr1,struct node **ptr2,struct node **ptr3){
struct node *gecici;
if(*ptr2==NULL && ptr1!=NULL){
*ptr3=*ptr1;
return;
}
else if(*ptr1==NULL && ptr2!=NULL){
*ptr3=*ptr2;
return ;
}
else if(*ptr1==NULL && ptr2==NULL){
return;
}
*ptr3=*ptr1;
gecici=*ptr1;
while(gecici->next!=NULL)
gecici=gecici->next;
gecici->next=*ptr2;
}
////////////////// main funct. ////////////////////////////
int main(){
struct node *ptr1,*ptr2,*ptr3=NULL;
ptr1=ptr2=NULL;
printf("First List : ");
insert_node(&ptr1,1);
insert_node(&ptr1,2);
insert_node(&ptr1,3);
print(ptr1);
printf("\nSecond List : ");
insert_node(&ptr2,6);
insert_node(&ptr2,7);
insert_node(&ptr2,8);
insert_node(&ptr2,9);
print(ptr2);
printf("\n Result :");
merge(&ptr1,&ptr2,&ptr3);
print(ptr3);
return 0;
}