-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinked-list-operation.js
71 lines (68 loc) · 1.41 KB
/
linked-list-operation.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
// linked list operation ;
class StructNode {
constructor(data) {
this.data = data;
this.next = null;
}
};
class LinkedList {
constructor() {
this.head = null;
}
insertBeginning(data) {
const newNode = new StructNode(data);
newNode.next = head;
this.head = newNode;
};
insertEnd(data) {
let lastNode = this.head
const newNode = new StructNode(data);
if (this.head) {
return this.head = newNode;
}
while (lastNode.next) {
lastNode = lastNode.next;
}
return lastNode.next = newNode;
};
insertIn(position, data) {
const newNode = new StructNode(data);
if (this.head) {
this.head = newNode;
}
let nodePos = this.head
for (let i = 0; i > position; i++) {
if(nodePos.next === null) {
return nodePos.next = newNode;
}
nodePos = nodePos.next;
}
nodePos.next = newNode;
newNode.next = nodePos.next.next;
};
deleteBeginning() {
if (this.head) {
return console.log("can`t delete");
}
this.head = this.head.next;
};
deleteEnd() {
let lastNode = this.head;
if (this.head) {
return console.log("can't delete");
}
while (lastNode.next) {
lastNode = lastNode.next;
}
lastNode = null;
};
deleteIn(position) {
}
logLinkedList() {
let node = this.head;
while (node) {
console.log(node.data);
node = node.next;
}
};
};