generated from codefellows/data-structures-and-algorithms
/
LinkeList.js
180 lines (142 loc) · 3.34 KB
/
LinkeList.js
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
'use strict';
const Node = require('./Node');
class LinkedList {
constructor() {
this.head = null;
this.length =0 ;
// tail
}
insertFirstNode(data) {
this.head = new Node(data, this.head);
this.length++;
}
insertLastNode(data) {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
} else {
let currentNode = this.head;
while (currentNode.next) {
currentNode = currentNode.next;
}
currentNode.next = newNode;
}
this.length++;
}
insertNodeAtIndex(data, index) {
if (this.length < index) {
console.log("index is greater than size of list");
return null ;
}
const node = new Node(data);
// if (!this.head) {
// // this.head = node;
// } else
if (index === 0) {
const head = this.head;
this.head = node;
node.next = head;
} else {
let previousNode;
let currentNode = this.head;
let indexCounter = 0;
while (indexCounter < index) {
previousNode = currentNode;
currentNode = previousNode.next;
indexCounter++;
}
previousNode.next = node;
node.next = null;
if (currentNode) {
node.next = currentNode;
}
}
this.length++;
}
getNodeAtIndex(index) {
if (index > this.length) {
console.log("index is greater than size of list");
return null;
}
let currentNode = this.head;
let counter = 0;
while (counter < index) {
currentNode = currentNode.next;
counter++;
}
// console.log(`Node data at index(${index}) is ${currentNode.data}`);
return currentNode.data ;
}
includes(values) {
let isIncludes = false;
let currentNode = this.head;
while (currentNode) {
if (currentNode.data === values) {
isIncludes = true;
}
currentNode = currentNode.next;
}
return isIncludes;
}
removeNodeAtIndex(index) {
if (index > this.length) {
console.log("index is greater than size of list");
return null;
}
if (!this.head) {
console.log("List is empty");
return null;
}
if (index === 0) {
this.head = null;
} else {
let previousNode;
let currentNode = this.head;
let counter = 0;
while (counter < index) {
previousNode = currentNode;
currentNode = previousNode.next;
counter++;
}
if (currentNode.next) {
previousNode.next = currentNode.next;
}
// else {
// previousNode.next = null;
// }
}
this.size--;
console.log(
`Removed node at index(${index}) and current linked list is: })`
);
}
clear() {
//for each of the nodes, remove at index
let index = 0;
while (index < this.length) {
console.log(`removing at index: ${index}`);
this.removeNodeAtIndex(index);
this.length--;
index++;
}
}
// printListData() {
// let currentNode = this.head;
// let output ='';
// while (currentNode) {
// output = ` ${output}${currentNode.data} -> ` ;
// currentNode = currentNode.next;
// }
// console.log(`${output}null`);
// }
toString() {
let currentNode = this.head;
let output ='';
while (currentNode) {
output = ` ${output}${currentNode.data} -> ` ;
currentNode = currentNode.next;
}
console.log(`${output}null`);
}
}
module.exports=LinkedList ;