-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
450 lines (417 loc) · 14.7 KB
/
index.html
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.tailwindcss.com"></script>
<title>Union and Intersection of two Linked Lists</title>
</head>
<body>
<nav
class="flex items-center justify-between z-50 flex-wrap bg-white px-5 pt-1 pb-1 lg:px-20 sticky top-0 text-center border-b border-gray-200 shadow-sm"
>
<div class="flex items-center flex-shrink-0 text-slate-800 mr-6 p-2">
<a href="">
<img src="logo.png" class="mr-2 h-8 w-8" alt="Logo" />
</a>
<a href="/" class="font-bold text-xl text-slate-800"
>Union and Intersection of two Linked Lists</a
>
</div>
<div class="lg:hidden">
<div
class="flex items-center px-3 py-2 border rounded text-slate-800 border-gray-200 hover:text-slate-500 hover:border-gray-500"
onclick="toggleMenu()"
>
<svg
class="fill-current h-3 w-3"
viewBox="0 0 20 20"
xmlns="http://www.w3.org/2000/svg"
>
<path d="M0 3h20v2H0V3zm0 6h20v2H0V9zm0 6h20v2H0v-2z" />
</svg>
</div>
</div>
<div
id="menu"
class="w-full hidden text-left lg:flex lg:items-center lg:w-auto"
>
<div class="text-md">
<a
href="/"
class="block mt-4 lg:inline-block lg:mt-0 text-slate-800 hover:text-slate-500 font-semibold mr-4"
>
Home
</a>
<a
href="#"
class="block mt-4 lg:inline-block lg:mt-0 text-slate-800 hover:text-slate-500 font-semibold mr-4"
>
About
</a>
<a
href="#"
class="block mt-4 lg:inline-block lg:mt-0 text-slate-800 hover:text-slate-500 font-semibold mr-4"
>
Contact
</a>
</div>
</div>
<div id="github" class="hidden lg:flex">
<a
href="https://github.com/sauravhathi/union-and-intersection-of-two-linked-lists"
class="inline-block font-bold px-4 py-3 leading-none rounded text-slate-50 hover:border-transparent bg-pink-500 hover:bg-pink-400 mt-4 lg:mt-0"
>
Github
</a>
</div>
</nav>
<div class="container mx-auto">
<div class="flex justify-center h-screen items-center -mt-10">
<div class="xl:w-1/3 lg:w-1/2 md:w-2/3 w-3/4">
<div class="mb-10">
<h1 class="text-2xl font-bold text-center text-slate-800">
Union and Intersection of two Linked Lists
</h1>
</div>
<div class="bg-slate-800 shadow-md rounded px-8 pt-6 pb-8 mb-4">
<div class="mb-4">
<label
class="block text-white text-sm font-bold mb-2"
for="list1"
>
Enter the List
</label>
<input
class="shadow appearance-none border rounded w-full py-2 px-3 text-slate-900 leading-tight focus:outline-none focus:shadow-outline"
id="list1"
name="list1"
type="text"
placeholder="e.g. 10 15 4 20"
onchange="reset()"
/>
<p class="text-slate-50 text-xs italic" id="list-error1"></p>
<label
class="block text-white text-sm font-bold mb-2 mt-2"
for="list2"
>
Enter the List
</label>
<input
class="shadow appearance-none border rounded w-full py-2 px-3 text-slate-900 leading-tight focus:outline-none focus:shadow-outline"
id="list2"
name="list2"
type="text"
placeholder="e.g. 8 4 2 10"
onchange="reset()"
/>
<p class="text-slate-50 text-xs italic" id="list-error2"></p>
</div>
<div class="flex items-center justify-end">
<button
class="bg-slate-600 hover:bg-slate-700 text-white font-semibold hover:text-white py-2 px-4 hover:border-transparent rounded"
type="submit"
id="submit"
onclick="findUnionAndIntersection()"
>
Submit
</button>
</div>
<div class="mt-4 text-left hidden" id="result-div">
<hr class="border-pink-400" />
<h1 class="text-xl text-white font-bold mt-2">Result</h1>
<div class="mt-4">
<p id="result"></p>
</div>
</div>
</div>
</div>
</div>
</div>
<footer
class="bg-slate-50 text-center text-sm p-4 bottom-0 border-t border-gray-200 w-full text-slate-800"
>
Made with ❤️ by
<a href="https://github.com/sauravhathi/">Saurav Hathi</a>
</footer>
<script>
// responsive menu bar
function toggleMenu() {
var menu = document.getElementById("menu");
var github = document.getElementById("github");
if (
menu.classList.contains("hidden") &&
github.classList.contains("hidden")
) {
menu.classList.remove("hidden");
github.classList.remove("hidden");
} else {
menu.classList.add("hidden");
github.classList.add("hidden");
}
}
// get value from input
function getList() {
var list1 = document.getElementById("list1").value;
var list2 = document.getElementById("list2").value;
// remove beginning and ending spaces and split the string
list1 = list1.trim().split(" ");
list2 = list2.trim().split(" ");
// if list is empty
list1 = list1.filter((item1) => item1 !== "");
list2 = list2.filter((item2) => item2 !== "");
// return the list
return [list1, list2];
}
// validate input
function validateArray(list1, list2) {
if (list1.length === 0) {
return false;
}
if (list2.length === 0) {
return false;
}
for (var i = 0; i < list1.length; i++) {
if (isNaN(list1[i])) {
return false;
}
}
for (var i = 0; i < list2.length; i++) {
if (isNaN(list2[i])) {
return false;
}
}
return true;
}
// onchange event of input
function reset() {
var [list1, list2] = getList();
var listError1 = document.getElementById("list-error1");
var listError2 = document.getElementById("list-error2");
var resultDiv = document.getElementById("result-div");
var result = document.getElementById("result");
if (!validateArray(list1, list2)) {
listError1.innerHTML = "Please enter a valid list";
listError2.innerHTML = "Please enter a valid list";
resultDiv.classList.add("hidden");
} else {
listError1.innerHTML = "";
listError2.innerHTML = "";
resultDiv.classList.add("hidden");
}
}
// find union and intersection
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
insert(data) {
const node = new Node(data);
if (this.head === null) {
this.head = node;
} else {
let current = this.head;
while (current.next !== null) {
current = current.next;
}
current.next = node;
}
}
// union of two linked lists
union(list) {
const unionList = new LinkedList();
const set = new Set();
let current = this.head;
while (current !== null) {
set.add(current.data);
current = current.next;
}
current = list.head;
while (current !== null) {
set.add(current.data);
current = current.next;
}
set.forEach((data) => {
unionList.insert(data);
});
return unionList;
}
// intersection of two linked lists
intersection(list) {
const intersectionList = new LinkedList();
const set = new Set();
let current = this.head;
while (current !== null) {
set.add(current.data);
current = current.next;
}
current = list.head;
while (current !== null) {
if (set.has(current.data)) {
intersectionList.insert(current.data);
}
current = current.next;
}
return intersectionList;
}
}
// findUnionAndIntersection function to find union and intersection of two linked lists and display the result on the page
function findUnionAndIntersection() {
var [list1, list2] = getList();
clr();
var listError1 = document.getElementById("list-error1");
var listError2 = document.getElementById("list-error2");
var resultDiv = document.getElementById("result-div");
var result = document.getElementById("result");
// validate input
if (!validateArray(list1, list2)) {
listError1.innerHTML = "Please enter a valid list";
listError2.innerHTML = "Please enter a valid list";
resultDiv.classList.add("hidden");
} else {
listError1.innerHTML = "";
listError2.innerHTML = "";
resultDiv.classList.remove("hidden");
// create two linked lists
const linkedList1 = new LinkedList();
const linkedList2 = new LinkedList();
// insert elements in linked lists
list1.forEach((data) => {
linkedList1.insert(data);
});
// insert elements in linked lists
list2.forEach((data) => {
linkedList2.insert(data);
});
// find union and intersection
const unionList = linkedList1.union(linkedList2);
const intersectionList = linkedList1.intersection(linkedList2);
var div = document.getElementById("result");
div.classList.add("grid", "grid-cols-1", "gap-4", "mt-4");
var hr = document.createElement("hr");
hr.classList.add("border-pink-400");
var ulAfterList1 = document.createElement("ul");
ulAfterList1.classList.add("flex", "flex-wrap","flex-cols-2", "gap-2");
var ulAfterList2 = document.createElement("ul");
ulAfterList2.classList.add("flex", "flex-wrap", "gap-2");
var ulAfterUnion = document.createElement("ul");
ulAfterUnion.classList.add("flex", "flex-wrap", "gap-2");
var ulAfterIntersection = document.createElement("ul");
ulAfterIntersection.classList.add("flex", "flex-wrap", "gap-2");
var h1BeforeList1 = document.createElement("h1");
h1BeforeList1.classList.add("text-md", "text-white", "font-semibold");
h1BeforeList1.innerHTML = "List 1: ";
var h1BeforeList2 = document.createElement("h1");
h1BeforeList2.classList.add("text-md", "text-white", "font-semibold");
h1BeforeList2.innerHTML = "List 2: ";
var h1BeforeUnion = document.createElement("h1");
h1BeforeUnion.classList.add("text-md", "text-white", "font-semibold");
h1BeforeUnion.innerHTML = "Union: ";
var h1BeforeIntersection = document.createElement("h1");
h1BeforeIntersection.classList.add(
"text-md",
"text-white",
"font-semibold"
);
h1BeforeIntersection.innerHTML = "Intersection: ";
// display list 1
let current = unionList.head;
while (current !== null) {
var li = document.createElement("li");
li.classList.add(
"flex",
"items-center",
"w-10",
"h-10",
"bg-slate-700",
"rounded",
"justify-around",
"text-white"
);
li.innerHTML = current.data;
ulAfterUnion.appendChild(li);
current = current.next;
}
// display list 2
current = intersectionList.head;
while (current !== null) {
var li = document.createElement("li");
li.classList.add(
"flex",
"items-center",
"w-10",
"h-10",
"bg-slate-700",
"rounded",
"justify-around",
"text-white"
);
li.innerHTML = current.data;
ulAfterIntersection.appendChild(li);
current = current.next;
}
// display union
current = linkedList1.head;
while (current !== null) {
var li = document.createElement("li");
li.classList.add(
"flex",
"items-center",
"w-10",
"h-10",
"bg-slate-700",
"rounded",
"justify-around",
"text-white"
);
li.innerHTML = current.data;
ulAfterList1.appendChild(li);
current = current.next;
}
// display intersection
current = linkedList2.head;
while (current !== null) {
var li = document.createElement("li");
li.classList.add(
"flex",
"items-center",
"w-10",
"h-10",
"bg-slate-700",
"rounded",
"justify-around",
"text-white"
);
li.innerHTML = current.data;
ulAfterList2.appendChild(li);
current = current.next;
}
div.appendChild(h1BeforeList1);
div.appendChild(ulAfterList1);
div.appendChild(h1BeforeList2);
div.appendChild(ulAfterList2);
div.appendChild(hr);
div.appendChild(h1BeforeUnion);
div.appendChild(ulAfterUnion);
div.appendChild(h1BeforeIntersection);
div.appendChild(ulAfterIntersection);
resultDiv.classList.remove("hidden");
}
}
// clear function to clear the result
function clr() {
var resultDiv = document.getElementById("result-div");
var result = document.getElementById("result");
result.innerHTML = "";
resultDiv.classList.add("hidden");
}
</script>
</body>
</html>