-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-push.js
More file actions
44 lines (40 loc) · 1021 Bytes
/
2-push.js
File metadata and controls
44 lines (40 loc) · 1021 Bytes
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
//pseudo
// 1. create a new node w val passed to the function
// 2. if the head property is null set the head and tail to be the newly created node.
// 3. if not, set the next property on the tail to be that node.
// 4. set the tail to be the newly created node.
// 5. increment the length
// 6. return the doubly linkd list
class Node {
constructor(val){
this.val = val;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList {
constructor(){
this.head = null;
this.tail = null;
this.length = 0;
}
push(val){
let newNode = new Node(val);
if(this.length === 0){
this.head = newNode
this.tail = newNode
}else{
this.tail.next = newNode;
newNode.prev = this.tail;
this.tail = newNode;
}
this.length ++
return this
}
}
list = new DoublyLinkedList()
list.push(90)
list.push(99)
list.push(100)
list.push("last item!")
console.log(list)