-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge-dll.c
175 lines (154 loc) · 3.78 KB
/
merge-dll.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#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* prev;
};
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**);
void traverse(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);
puts("");
check_intersection(&head1 , &head2);
struct Node* head_merged = merge(&head1 , &head2);
printf("\nMerged List : ");
display(&head_merged);
printf("\nPress a key to move on to traversal\n");
getchar();
if(getchar()) {
traverse(&head_merged);
}
}
struct Node* create_node(int data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->next = NULL;
new_node->prev = NULL;
new_node->data = data;
return new_node;
}
void input(struct Node** head , int size) {
while(size--) {
printf("Add data : ");
int data;
scanf("%d" , &data);
struct Node* new_node = create_node(data);
if(*head == NULL) {
*head = new_node;
}else{
struct Node* current = *head;
while(current->next != NULL) {
current = current->next;
}
current->next = new_node;
new_node->prev = current;
}
}
}
struct Node* merge(struct Node** head1 , struct Node** head2) {
struct Node* current = *head1;
while(current->next != NULL) {
current = current->next;
}
current->next = *head2;
(*head2)->prev = current;
return *head1;
}
void check_intersection(struct Node** head1 , struct Node** head2) {
struct Node* current1 = *head1;
int index1 = 0 , flag = 0;
while(current1 != NULL) {
index1++;
struct Node* current2 = *head2;
int index2 = 0;
while(current2 != NULL) {
index2++;
if(current1->data == current2->data) {
flag = 1;
printf("Node %d of list1 intersects with Node %d of list2\n" , index1 , index2);
}
current2 = current2->next;
}
current1 = current1->next;
}
if(flag == 0) {
printf("No intersections found\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");
}
void traverse(struct Node** head) {
struct Node* current = *head;
struct Node* null_prev = NULL;
while(1) {
clrscr();
puts("Traverse List :\n");
printf("Current Element : ");
if(current != NULL) {
printf("%d" , current->data);
}else{
printf("NULL");
}
puts("\nPress [n] to move to next node\nPress [p] to move to previous node\nPress [e] to exit");
char ch = getchar();
if(current == NULL) {
if(null_prev == *head) {
if(ch == 'n' || ch == 'N') {
current = null_prev;
}
}else{
if(ch == 'p' || ch == 'P') {
current = null_prev;
}
}
if(ch == 'e' || ch == 'E') {
clrscr();
puts("Traversal Terminated ...");
return;
}
}else{
if(ch == 'n' || ch == 'N') {
null_prev = current;
current = current->next;
}else if(ch == 'p' || ch == 'P') {
null_prev = current;
current = current->prev;
}else if(ch == 'e' || ch == 'E') {
clrscr();
puts("Traversal Terminated ...");
return;
}
}
}
}