-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink_list.js
More file actions
139 lines (112 loc) · 2.76 KB
/
link_list.js
File metadata and controls
139 lines (112 loc) · 2.76 KB
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
// class Node {
// constructor(value) {
// this.value = value;
// this.next = null;
// }
// }
// const head = new Node(29);
// head.next = new Node(30);
// head.next.next = new Node(31);
// let temp = head;
// while (temp !== null) {
// console.log(temp.value, " ");
// temp = temp.next;
// }
// console.log(head);
// ! link list part 2
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class Link_List {
constructor() {
this.head = null;
this.tail = null;
this.length = null;
}
//? start append logic
append(value) {
//if the link list is empty
const newNode = new Node(value);
if (this.head === null) {
this.head = newNode;
this.tail = newNode;
} else {
// if the link list in not empty ?
this.tail.next = newNode;
this.tail = newNode;
}
this.length++;
}
// ? start prepend logic
prepend(value) {
const newNode = new Node(value);
if (this.head === null) {
this.head = newNode;
this.tail = newNode;
} else {
// if the link list in not empty ?
newNode.next = this.head;
this.head = newNode;
}
}
// ? start insert logic
insert(index, value) {
// check input index is less then this.length
if (index < 0 || index > this.length) {
console.error("index out of bound :");
return undefined;
}
// * if the insert is in the start of the linked list
if (index === 0) {
return this.prepend(value);
}
// * in the insert is in the end of the linked list
if (index === this.length) {
return this.append(value);
}
//* if the insert is in the middle or the linked list
//? find the leading node
const leadingNode = this._traverseToIndex(index - 1);
const holdingNode = leadingNode.next;
const newNode = new Node(value);
leadingNode.nex = newNode;
newNode.next = holdingNode;
this.length++;
}
//* reusable function insert and remove / privet helper method
_traverseToIndex(index) {
let count = 0;
let currentNode = this.head;
while (count !== index) {
currentNode = currentNode.next;
count++;
}
return currentNode;
}
// ? start remove logic
remove() {}
//? start print
print() {
let currentNode = this.head;
while (currentNode !== null) {
console.log("currentNode :", currentNode.value);
currentNode = currentNode.next;
}
}
}
const linkList = new Link_List();
// link list append check
linkList.append(0);
linkList.append(1);
linkList.append(3);
linkList.insert(2, 200);
//* link list prepend check
// linkList.prepend(20);
// linkList.prepend(30);
// linkList.prepend(40);
// insert the index and value in link list
// linkList.insert(2, 199);
linkList.print();