|
| 1 | +import { defaultEquals } from '../util'; |
| 2 | +import LinkedList from './linked-list'; |
| 3 | +import { DoublyNode } from './models/linked-list-models'; |
| 4 | + |
| 5 | +export default class DoublyLinkedList extends LinkedList { |
| 6 | + constructor(equalsFn = defaultEquals) { |
| 7 | + super(equalsFn); |
| 8 | + } |
| 9 | + push(element) { |
| 10 | + const node = new DoublyNode(element); |
| 11 | + if (this.head == null) { |
| 12 | + this.head = node; |
| 13 | + this.tail = node; // NEW |
| 14 | + } else { |
| 15 | + // attach to the tail node // NEW |
| 16 | + this.tail.next = node; |
| 17 | + node.prev = this.tail; |
| 18 | + this.tail = node; |
| 19 | + } |
| 20 | + this.count++; |
| 21 | + } |
| 22 | + insert(element, index) { |
| 23 | + if (index >= 0 && index <= this.count) { |
| 24 | + const node = new DoublyNode(element); |
| 25 | + let current = this.head; |
| 26 | + if (index === 0) { |
| 27 | + if (this.head == null) { |
| 28 | + // NEW |
| 29 | + this.head = node; |
| 30 | + this.tail = node; |
| 31 | + } else { |
| 32 | + node.next = this.head; |
| 33 | + this.head.prev = node; // NEW |
| 34 | + this.head = node; |
| 35 | + } |
| 36 | + } else if (index === this.count) { |
| 37 | + // last item // NEW |
| 38 | + current = this.tail; // {2} |
| 39 | + current.next = node; |
| 40 | + node.prev = current; |
| 41 | + this.tail = node; |
| 42 | + } else { |
| 43 | + const previous = this.getElementAt(index - 1); |
| 44 | + current = previous.next; |
| 45 | + node.next = current; |
| 46 | + previous.next = node; |
| 47 | + current.prev = node; // NEW |
| 48 | + node.prev = previous; // NEW |
| 49 | + } |
| 50 | + this.count++; |
| 51 | + return true; |
| 52 | + } |
| 53 | + return false; |
| 54 | + } |
| 55 | + removeAt(index) { |
| 56 | + if (index >= 0 && index < this.count) { |
| 57 | + let current = this.head; |
| 58 | + if (index === 0) { |
| 59 | + this.head = this.head.next; // {1} |
| 60 | + // if there is only one item, then we update tail as well //NEW |
| 61 | + if (this.count === 1) { |
| 62 | + // {2} |
| 63 | + this.tail = undefined; |
| 64 | + } else { |
| 65 | + this.head.prev = undefined; // {3} |
| 66 | + } |
| 67 | + } else if (index === this.count - 1) { |
| 68 | + // last item //NEW |
| 69 | + current = this.tail; // {4} |
| 70 | + this.tail = current.prev; |
| 71 | + this.tail.next = undefined; |
| 72 | + } else { |
| 73 | + current = this.getElementAt(index); |
| 74 | + const previous = current.prev; |
| 75 | + // link previous with current's next - skip it to remove |
| 76 | + previous.next = current.next; // {6} |
| 77 | + current.next.prev = previous; // NEW |
| 78 | + } |
| 79 | + this.count--; |
| 80 | + return current.element; |
| 81 | + } |
| 82 | + return undefined; |
| 83 | + } |
| 84 | + indexOf(element) { |
| 85 | + let current = this.head; |
| 86 | + let index = 0; |
| 87 | + while (current != null) { |
| 88 | + if (this.equalsFn(element, current.element)) { |
| 89 | + return index; |
| 90 | + } |
| 91 | + index++; |
| 92 | + current = current.next; |
| 93 | + } |
| 94 | + return -1; |
| 95 | + } |
| 96 | + getHead() { |
| 97 | + return this.head; |
| 98 | + } |
| 99 | + getTail() { |
| 100 | + return this.tail; |
| 101 | + } |
| 102 | + clear() { |
| 103 | + super.clear(); |
| 104 | + this.tail = undefined; |
| 105 | + } |
| 106 | + toString() { |
| 107 | + if (this.head == null) { |
| 108 | + return ''; |
| 109 | + } |
| 110 | + let objString = `${this.head.element}`; |
| 111 | + let current = this.head.next; |
| 112 | + while (current != null) { |
| 113 | + objString = `${objString},${current.element}`; |
| 114 | + current = current.next; |
| 115 | + } |
| 116 | + return objString; |
| 117 | + } |
| 118 | + inverseToString() { |
| 119 | + if (this.tail == null) { |
| 120 | + return ''; |
| 121 | + } |
| 122 | + let objString = `${this.tail.element}`; |
| 123 | + let previous = this.tail.prev; |
| 124 | + while (previous != null) { |
| 125 | + objString = `${objString},${previous.element}`; |
| 126 | + previous = previous.prev; |
| 127 | + } |
| 128 | + return objString; |
| 129 | + } |
| 130 | +} |
0 commit comments