-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge-sll.c
112 lines (95 loc) · 2.26 KB
/
merge-sll.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
#include <stdio.h>
#include<stdlib.h>
#ifdef _WIN32
#include<conio.h>
#else
#define clrscr() printf("\e[1;1H\e[2J")
#endif
struct Node{
int data;
struct Node* next;
};
struct Node* create_node(int);
void input(struct Node** , int);
struct Node* merge(struct Node** , struct Node**);
void check_intersection(struct Node** , struct Node**);
void display(struct Node**);
int main() {
struct Node* head1 = NULL;
struct Node* head2 = NULL;
int size1 = 0 , size2 = 0;
puts("Input of Singly Linked List 1 :");
printf("Size : ");
scanf("%d" , &size1);
input(&head1 , size1);
puts("\nInput of Singly Linked List 2 :");
printf("Size : ");
scanf("%d" , &size2);
input(&head2 , size2);
clrscr();
printf("List 1 : ");
display(&head1);
printf("List 2 : ");
display(&head2);
check_intersection(&head1 , &head2);
struct Node* head_merged = merge(&head1 , &head2);
printf("\nMerged List : ");
display(&head_merged);
}
struct Node* create_node(int data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->next = NULL;
new_node->data = data;
return new_node;
}
void input(struct Node** head , int size) {
while(size--) {
int data;
printf("Add data : ");
scanf("%d" , &data);
struct Node* new_node = create_node(data);
if(*head != NULL) {
new_node->next = *head;
}
*head = new_node;
}
}
struct Node* merge(struct Node** head1 , struct Node** head2) {
struct Node* current = *head1;
while(current->next != NULL) {
current = current->next;
}
current->next = *head2;
return *head1;
}
void check_intersection(struct Node** head1 , struct Node** head2) {
printf("\n");
struct Node* current1 = *head1;
int node1 = 0 , flag = 0;
while(current1 != NULL) {
node1++;
struct Node* current2 = *head2;
int node2 = 0;
while(current2 != NULL) {
node2++;
if(current1->data == current2->data) {
printf("Node %d of List1 and Node %d of List2 intersect\n" , node1 , node2);
flag = 1;
}
current2 = current2->next;
}
current1 = current1->next;
}
if(flag == 0) {
puts("No intersecting nodes\n");
}
}
void display(struct Node** head) {
struct Node* current = *head;
printf("HEAD->");
while(current != NULL) {
printf("%d->" , current->data);
current = current->next;
}
printf("NULL\n");
}