|
| 1 | +class Node{ |
| 2 | + constructor(val){ |
| 3 | + this.val = val; |
| 4 | + this.next = null; |
| 5 | + this.prev = null; |
| 6 | + } |
| 7 | +} |
| 8 | + |
| 9 | + |
| 10 | +class DoublyLinkedList { |
| 11 | + constructor(){ |
| 12 | + this.head = null; |
| 13 | + this.tail = null; |
| 14 | + this.length = 0; |
| 15 | + } |
| 16 | + push(val){ |
| 17 | + var newNode = new Node(val); |
| 18 | + if(this.length === 0){ |
| 19 | + this.head = newNode; |
| 20 | + this.tail = newNode; |
| 21 | + } else { |
| 22 | + this.tail.next = newNode; |
| 23 | + newNode.prev = this.tail; |
| 24 | + this.tail = newNode; |
| 25 | + } |
| 26 | + this.length++; |
| 27 | + return this; |
| 28 | + } |
| 29 | + pop(){ |
| 30 | + if(!this.head) return undefined; |
| 31 | + var poppedNode = this.tail; |
| 32 | + if(this.length === 1){ |
| 33 | + this.head = null; |
| 34 | + this.tail = null; |
| 35 | + } else { |
| 36 | + this.tail = poppedNode.prev; |
| 37 | + this.tail.next = null; |
| 38 | + poppedNode.prev = null; |
| 39 | + } |
| 40 | + this.length--; |
| 41 | + return poppedNode; |
| 42 | + } |
| 43 | + shift(){ |
| 44 | + if(this.length === 0) return undefined; |
| 45 | + var oldHead = this.head; |
| 46 | + if(this.length === 1){ |
| 47 | + this.head = null; |
| 48 | + this.tail = null; |
| 49 | + }else{ |
| 50 | + this.head = oldHead.next; |
| 51 | + this.head.prev = null; |
| 52 | + oldHead.next = null; |
| 53 | + } |
| 54 | + this.length--; |
| 55 | + return oldHead; |
| 56 | + } |
| 57 | + unshift(val){ |
| 58 | + var newNode = new Node(val); |
| 59 | + if(this.length === 0) { |
| 60 | + this.head = newNode; |
| 61 | + this.tail = newNode; |
| 62 | + } else { |
| 63 | + this.head.prev = newNode; |
| 64 | + newNode.next = this.head; |
| 65 | + this.head = newNode; |
| 66 | + } |
| 67 | + this.length++; |
| 68 | + return this; |
| 69 | + } |
| 70 | + get(index){ |
| 71 | + if(index < 0 || index >= this.length) return null; |
| 72 | + var count, current; |
| 73 | + if(index <= this.length/2){ |
| 74 | + count = 0; |
| 75 | + current = this.head; |
| 76 | + while(count !== index){ |
| 77 | + current = current.next; |
| 78 | + count++; |
| 79 | + } |
| 80 | + } else { |
| 81 | + count = this.length - 1; |
| 82 | + current = this.tail; |
| 83 | + while(count !== index){ |
| 84 | + current = current.prev; |
| 85 | + count--; |
| 86 | + } |
| 87 | + } |
| 88 | + return current; |
| 89 | + } |
| 90 | + set(index, val){ |
| 91 | + var foundNode = this.get(index); |
| 92 | + if(foundNode != null){ |
| 93 | + foundNode.val = val; |
| 94 | + return true; |
| 95 | + } |
| 96 | + return false; |
| 97 | + } |
| 98 | + insert(index, val){ |
| 99 | + if(index < 0 || index > this.length) return false; |
| 100 | + if(index === 0) return !!this.unshift(val); |
| 101 | + if(index === this.length) return !!this.push(val); |
| 102 | + |
| 103 | + var newNode = new Node(val); |
| 104 | + var beforeNode = this.get(index-1); |
| 105 | + var afterNode = beforeNode.next; |
| 106 | + |
| 107 | + beforeNode.next = newNode, newNode.prev = beforeNode; |
| 108 | + newNode.next = afterNode, afterNode.prev = newNode; |
| 109 | + this.length++; |
| 110 | + return true; |
| 111 | + } |
| 112 | + remove(index) { |
| 113 | + if (index < 0 || index >= this.length) return false; |
| 114 | + if(index === 0) return !!this.shift(); |
| 115 | + if (index === this.length) return !!this.pop(); |
| 116 | + |
| 117 | + var removeNode = this.get(index); |
| 118 | + var beforeNode = removeNode.prev; |
| 119 | + var afterNode = removeNode.next; |
| 120 | + |
| 121 | + beforeNode.next = afterNode; |
| 122 | + afterNode.prev = beforeNode; |
| 123 | + |
| 124 | + removeNode.next = null; |
| 125 | + removeNode.prev = null; |
| 126 | + |
| 127 | + this.length--; |
| 128 | + return removeNode; |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +var list = new DoublyLinkedList() |
| 133 | +list.push("Harry") |
| 134 | +list.push("Ron") |
| 135 | +list.push("Hermione") |
0 commit comments