-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbox.js
82 lines (74 loc) · 1.88 KB
/
box.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
class Box{
constructor(){
this.vertices = [];
this.edges = [];
this.faces = [];
this.l1 = null;
this.l2 = null;
this.l3 = null;
this.l4 = null;
}
vertices(l1,l2,l3,l4){
this.l1 = l1;
this.l2 = l2;
this.l3 = l3;
this.l4 = l4;
}
edges(){
this.edges.push(this.l1);
this.edges.push(this.l2);
this.edges.push(this.l3);
this.edges.push(this.l4);
}
faces(){
this.faces.push(this.l1);
this.faces.push(this.l2);
this.faces.push(this.l3);
this.faces.push(this.l4);
}
// insert at a given index
insert(index, value){
if(index >= this.length){
return this.append(value);
}
const newNode = {value: value, next: null};
const leader = this.traverseToIndex(index-1);
const holdingPointer = leader.next;
leader.next = newNode;
newNode.next = holdingPointer;
this.length++;
}
// remove at a given index
remove(index){
if(index >= this.length){
return;
}
const leader = this.traverseToIndex(index-1);
const unwantedNode = leader.next;
leader.next = unwantedNode.next;
this.length--;
}
// traverse to a given index
traverseToIndex(index){
let counter = 0;
let currentNode = this.head;
while(counter !== index){
currentNode = currentNode.next;
counter++;
}
return currentNode;
}
// print the linked list
printList(){
const array = [];
let currentNode = this.head;
while(currentNode !== null){
array.push(currentNode.value);
currentNode = currentNode.next;
}
return array;
}
}
// Path: line.js
const box = new Box();
console.log(box.printList())