-
Notifications
You must be signed in to change notification settings - Fork 0
/
singLinkedList.js
224 lines (196 loc) · 5.76 KB
/
singLinkedList.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
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
'use strict';
class Node{ // >-An object with data and a pointer->
constructor(entry){
this._entry = entry;
this._next = null; // <-Single pointer, points to the next object in line-<
Object.seal(this);
}
get entry(){ return this._entry; }
set entry(value){ this._entry = value; }
get next(){ return this._next; }
set next(value){ this._next = value; }
}
class SingLinkList{ // >>-Singly Linked List->>
constructor(){
this._head = null; // <-List Head by which the list is referenced-<
this._size = 0;
}
get head(){ return this._head; }
set head(node){ this._head = node; }
get size(){ return this._size; }
set size(value){ this._size = value; }
// >>=Primary Methods=<<
//
pin(value) { // <<-Pins an entry to the beginning of the list-<<
if (this.head === null){ // <--Iz the list is empty? |>
this.head = new Node(value); // then the value is added as the head-<
} else {
const temp = this.head; // <--Stores the old head-<
this.head = new Node(value); // <--Sets new node as the new head-<
this.head.next = temp; // <--Points the new head to what is now its previous entry-<
}
this.size += 1;
}
push(value){
const node = new Node(value);
if (this.head === null){
this.head = node;
} else {
let currentNode = this.head;
while (currentNode){
if (currentNode.next === null){
currentNode.next = node;
node.next = null;
break;
}
currentNode = currentNode.next;
}
}
this.size += 1;
}
millet() { // <<-Removes the head and returns its contents-<<
if (this.head !== null){
const result = this.head.entry; // <--Stores the head-<
const newHead = this.head.next;
this.head = undefined;
this.head = newHead; // <--New head-<
this.size -= 1;
return result;
}
console.error('There seems to be no list here');
return -1;
}
pop(){
let currentNode = this.head,
newEnd;
while (currentNode.next){
newEnd = currentNode;
currentNode = currentNode.next;
}
if (currentNode.next === null){
newEnd.next = null;
}
}
extract(value) {
let currentNode = this.head;
if (currentNode.entry === value){ // <--Is the value pointing to the head? |>
this.head = currentNode.next; // |> then dereference it-<
this.size -= 1;
return value;
}
let previousNode = currentNode;
while (currentNode.next){
if (currentNode.entry === value){
previousNode.next = currentNode.next;
previousNode = currentNode;
currentNode = currentNode.next;
break;
}
previousNode = currentNode;
currentNode = currentNode.next;
}
if (currentNode.entry === value){
previousNode.next = null;
}
this.size -= 1;
return value;
}
cullDupes(){
const record = {};
let check = this.head,
previous = null;
while (check){
if (record[check.entry]){
previous.next = check.next;
this.size -= 1;
} else {
record[check.entry] = true;
previous = check;
}
check = check.next;
}
}
insertEntryAt(entry, index) { // >>-Inserts an Entry at the specified position Index->>
if (index > 0 && index > this.size){ // <<=Checks if Index is a valid position=<<
console.error("There aren't so many entries in the list.");
return false;
}
const node = new Node(entry);
let current = this.head,
previous;
if (index === 0){ // <<=Add Entry to the first Index (making it the Head)=<<
node.next = this.head;
this.head = node;
} else {
current = this.head;
let it = 0;
while (it < index){ // <<=Iterates to find the insertion point at Index=<<
it += 1;
previous = current;
current = current.next;
}
node.next = current; // <<=Add Entry=<<
previous.next = node;
}
this.size += 1;
}
insertAfter(target, value){
const node = new Node(value);
let currentNode = this.head;
while (currentNode){
if (currentNode.entry === target){
node.next = currentNode.next;
currentNode.next = node;
break;
}
currentNode = currentNode.next;
}
this.size += 1;
}
invert(){
let currentNode = this.head, // <--Start at the head-<
previousNode = null;
while (currentNode){
const next = currentNode.next; // <--The loop looks ahead at the next pointer-<
currentNode.next = previousNode; // <--On first itt. the head's 'next' is rereferenced to NULL, and what was the head will be passed forward to be the next rereference point-<
if (next === null) break; // <--If next is pointing to NULL, we know to not go there-<
previousNode = currentNode;
currentNode = next;
}
this.head = currentNode;
}
// >>=Helper Methods=<<
//
isEmpty() { return this.size === 0; } // <<-Check if list is empty-<<
listSize() { return this.size; } // <<-List Size check-<<
scry() { return this.head; }
indexOf(entry) { // >>-Find Index of an Entry->>
let count = 0,
current = this.head;
while (current){
if (current.entry === entry) return count;
count += 1;
current = current.next;
}
}
doesContain(value){ // >>-Checks if a given value is contained by the list->>
let currentNode = this.head;
while (currentNode){
if (currentNode.entry === value){
return true;
}
currentNode = currentNode.next;
}
return false;
}
listContents() { // <<-Returns List as an array-<<
let current = this.head;
const elements = [];
while (current) {
elements.push(current.entry);
current = current.next;
}
return elements;
}
}
module.exports = SingLinkList;