-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathlink_list.html
81 lines (78 loc) · 1.91 KB
/
link_list.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
<html>
<head>
<title>Linked List in JavaScript</title>
<script>
class List {
constructor(data) {
this.head = {
value: data,
next: null,
};
this.tail = this.head;
this.size = 1;
}
appendNode(nodeData) {
let newNode = {
value: nodeData,
next: null,
};
this.tail.next = newNode;
this.tail = newNode;
this.size += 1;
}
traversing() {
let counter = 0;
let currentNode = this.head;
while (counter < this.size) {
// console.warn(currentNode);
currentNode = currentNode.next;
counter++;
}
}
deleteNode(index) {
let counter = 1;
let lead = this.head;
if (index === 1) {
this.head = this.head.next;
} else {
while (counter < index - 1) {
lead = lead.next;
counter++;
}
let nextNode = lead.next.next;
lead.next = nextNode;
console.warn(lead);
}
}
searchNode(data){
let result = undefined;
let lead= this.head;
let loop=true;
while(loop){
lead=lead.next;
// console.warn(lead)
loop = !!lead;
if(loop && lead.value === data){
loop=false;
result=lead;
}
}
console.warn(result)
}
}
let list = new List(200);
list.appendNode(300);
list.appendNode(400);
list.appendNode(500);
list.appendNode(600);
list.appendNode(700);
list.traversing();
list.deleteNode(1);
list.searchNode(900)
// console.warn(list);
</script>
</head>
<body>
<h1>Linked List in JavaScript</h1>
</body>
</html>