Skip to content

Commit 51598a9

Browse files
committed
Linked List All
1 parent 4a5a17b commit 51598a9

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

LinkedList/server.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,115 @@ const PORT = process.env.PORT || 8000
44
const createServer = http.createServer()
55

66

7+
// Steps
8+
9+
// 1- When Firs node is added it will be our head node
10+
// 2- We somehow need to traverse and moveon the next step , but we also have to keep the last node PointerEvent, so that when ever we are adding new node, we can have the last one and then rplace that , and this way it will go next next and next
11+
12+
13+
14+
// Right now we have the last value in our tail, so we will smply change the tale
15+
16+
17+
18+
class LinkedList{
19+
constructor(value){
20+
this.head = {
21+
value : value ,
22+
next : null
23+
}
24+
this.tail = this.head
25+
this.length= 1
26+
}
27+
28+
append(value){
29+
let node = {
30+
value: value,
31+
next:null
32+
}
33+
this.tail.next = node
34+
this.tail = node
35+
this.length++
36+
return this.head
37+
}
38+
39+
prepend(value){
40+
let node = {
41+
value: value,
42+
next:null
43+
}
44+
node.next = this.head
45+
this.head = node
46+
this.length++
47+
}
48+
49+
insert(index, value){
50+
let node = {
51+
value: value,
52+
next:null
53+
}
54+
if(index == 0){
55+
this.prepend(value)
56+
return
57+
}
58+
let prevNode;
59+
let currentNode = this.head
60+
for(let i=0;i<index; i++){
61+
prevNode = currentNode
62+
currentNode = currentNode.next
63+
}
64+
prevNode.next = node
65+
node.next = currentNode
66+
this.length++
67+
}
68+
69+
delete(value){
70+
let preNode;
71+
let currentNode = this.head
72+
while(currentNode != null){
73+
if(currentNode.next.value === value){
74+
preNode = currentNode
75+
preNode.next = currentNode.next
76+
delete currentNode;
77+
this.length--
78+
return
79+
}
80+
currentNode = currentNode.next
81+
}
82+
console.log("check", preNode, currentNode)
83+
}
84+
85+
printValues(){
86+
const arr = []
87+
let currentNode = this.head
88+
while(currentNode !== null){
89+
arr.push(currentNode.value)
90+
currentNode = currentNode.next
91+
}
92+
console.log("List : ", arr)
93+
console.log("LinkedList : ", this.head)
94+
}
95+
96+
}
97+
98+
const mylink = new LinkedList(5)
99+
100+
mylink.append(10)
101+
102+
mylink.delete(10)
103+
104+
mylink.printValues()
105+
106+
107+
108+
109+
110+
111+
112+
113+
114+
115+
7116

8117

9118

0 commit comments

Comments
 (0)