Skip to content

Commit dbb7064

Browse files
author
Sheary Tan
committed
linked-list added
1 parent 982334e commit dbb7064

File tree

1 file changed

+239
-0
lines changed

1 file changed

+239
-0
lines changed
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
// Implementation of single link list
2+
// Author: Sheary Tan
3+
4+
class Node {
5+
constructor(val) {
6+
this.val = val;
7+
this.next = null;
8+
}
9+
}
10+
11+
class SinglyList {
12+
constructor() {
13+
// Initializing...
14+
// There is nothing at the beginning so
15+
// the head & the tail should be empty,
16+
// and the length should be zero too
17+
this.head = null;
18+
this.tail = null;
19+
this.length = 0;
20+
}
21+
22+
// 1st method: push (push the val into the end of the list)
23+
push(val) {
24+
// Firstly, we have to accept a new value
25+
var newNode = new Node(val);
26+
// If there is no head, means there is nothing on the list,
27+
// set the head & tail pointers to this value.
28+
if(!this.head) {
29+
this.head = newNode;
30+
this.tail = newNode;
31+
} else {
32+
// If there is value existed, assign the newNode to the next value
33+
// of the current existing value
34+
this.tail.next = newNode;
35+
// Update the tail to it!
36+
this.tail = newNode;
37+
}
38+
// Increment the length and return the newly created link-list
39+
this.length++;
40+
return this;
41+
}
42+
43+
// 2nd method: pop (remove the last item)
44+
pop() {
45+
// If there is nothing on the list, return undefined
46+
if (!this.head) return undefined;
47+
// Loop through to find the last item
48+
var current = this.head;
49+
var newTail = current;
50+
while(current.next) {
51+
newTail = current;
52+
current = current.next
53+
}
54+
// Set the found tail to tail
55+
this.tail = newTail;
56+
// The next one will be null since we
57+
// would like to remove it
58+
this.tail.next = null;
59+
// Decrease the length
60+
this.length--;
61+
// If there is nothing left after removing
62+
// set the head and tail to be null
63+
if(this.length === 0) {
64+
this.head = null;
65+
this.tail = null;
66+
}
67+
return current;
68+
}
69+
70+
// 3rd method: shift (remove the first item)
71+
shift() {
72+
// If there is no nothing on the list, return undefined
73+
if(!this.head) return undefined;
74+
// Store the current head in a new variable
75+
var currentHead = this.head;
76+
// Shift the head to the next value of the currentHead
77+
this.head = currentHead.next;
78+
// Decrement the length since we have removed the head
79+
this.length--;
80+
if(this.length === 0) {
81+
this.head = null;
82+
this.tail = null;
83+
}
84+
return currentHead;
85+
}
86+
87+
// 4th method: unshift (add item to the beginning )
88+
unshift(val) {
89+
// Initialize the value to a new node;
90+
var newNode = new Node(val);
91+
// If there is nothing on the list, set the head and tail
92+
// point to that new node
93+
if(!this.head) {
94+
this.head = newNode;
95+
this.tail = newNode;
96+
} else {
97+
// In order to set the head value points to the head,
98+
// we have to set the current head to the next item of newNode
99+
newNode.next = this.head;
100+
// and now we can assign the head to newNode!
101+
this.head = newNode
102+
}
103+
// Increment the length after adding the new head
104+
this.length++;
105+
return this;
106+
}
107+
108+
// 5th method: get
109+
get(i) {
110+
// get accepts an index in and find the specific value
111+
// If i is less than zero, or greater and equal to the length
112+
// of the current link list, return null;
113+
if(i < 0 || i >= this.length) return null;
114+
// Use the counter to find the value
115+
var counter = 0;
116+
var current = this.head;
117+
while(counter !== i) {
118+
current = current.next;
119+
counter++
120+
}
121+
// Found it and return
122+
return current
123+
}
124+
125+
// 6th method: set
126+
// Use the given index to find the target value and change it with the new
127+
// given value
128+
set(i, val) {
129+
var targetNode = this.get(i);
130+
if(targetNode) {
131+
targetNode.val = val;
132+
return true;
133+
}
134+
return false;
135+
}
136+
137+
// 8th method: remove (from any index)
138+
remove(i) {
139+
// Check if there's anything on the list
140+
if(i < 0 || i >= this.length) return undefined;
141+
if(i === 0) return this.shift();
142+
if(i === this.length - 1) return this.pop();
143+
// Get the previous item of the target
144+
var previousTarget = this.get(i - 1);
145+
// Since we've got the previous item of the target,
146+
// we now can remove the target by setting the next item of previousTarget
147+
// to the next item of "removed"
148+
var removed = previousTarget.next;
149+
previousTarget.next = removed.next
150+
// Decrease the length
151+
this.length--;
152+
return removed;
153+
}
154+
155+
// 9th method: reverse
156+
reverse() {
157+
// Firstly we have to set the head to the tail,
158+
// and the tail to the head
159+
var target = this.head;
160+
this.head = this.tail;
161+
this.tail = target;
162+
// Now we have to re-arrange those next & previous
163+
// items of the list
164+
var next;
165+
var prev = null;
166+
// Loop through every single item on the list
167+
for(let i = 0; i < this.length; i++) {
168+
// This part is a bit confusing, let's take an example
169+
// Example = [1, 2, 3, 4, 5], T = target, P = previous, N = next
170+
next = target.next;
171+
// Firstly, the target is 1
172+
// [1, 2, 3, 4, 5]
173+
// T
174+
// And target.next is 2
175+
// [1, 2, 3, 4, 5]
176+
// T N
177+
target.next = prev;
178+
// Remember our target is 1 & prev is null? Now it said the next item of
179+
// 1 is null,
180+
// 1 -> null
181+
prev = target;
182+
// Now we're setting the "prev = null" to the target value
183+
// which now equals to "prev = 1"
184+
// [1, 2, 3, 4, 5]
185+
// P N
186+
target = next;
187+
// And lastly we set the "next" value to be T
188+
// [1, 2, 3, 4, 5]
189+
// P T
190+
191+
192+
// So the cycle repeats again, now let's go through 1 more cycle
193+
// [1, 2, 3, 4, 5]
194+
// P T
195+
// next = target.next
196+
// [1, 2, 3, 4, 5]
197+
// P T N
198+
// target.next = prev
199+
// Remember we have the list (1 -> null)?
200+
// now target.next = prev means the next item of 2 is 1
201+
// 2 -> 1 -> null
202+
// prev = target
203+
// Now 2 is prev
204+
// [1, 2, 3, 4, 5]
205+
// P N
206+
// target = next
207+
// And 3 is now the target
208+
// [1, 2, 3, 4, 5]
209+
// P T
210+
// And the cycle repeats again until the target reach the end of the list...
211+
}
212+
return this;
213+
}
214+
215+
// 10th method: insert
216+
insert(i, val) {
217+
// Check the requirements
218+
if(i < 0 || i >= this.length) return false;
219+
if(i === this.length) return !!this.push(val);
220+
if(i === 0) return !!this.unshift(val);
221+
222+
// Initialize a new node
223+
var newNode = new Node(val);
224+
// Get the previous node of newNode
225+
var prevNode = this.get(i - 1);
226+
// Remember we haven't insert the newNode into the list!
227+
// And also store the next item of prevNode in a temporary variable
228+
// because we are going to set it to the next item of newNode later
229+
var temp = prevNode.next;
230+
// Now we can set the next item of prevNode to newNode
231+
prevNode.next = newNode;
232+
// Then the next item of newNode will be the temporary item we store
233+
newNode.next = temp;
234+
// Increment the length
235+
this.length++;
236+
return true;
237+
}
238+
239+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy