|
| 1 | +let LinkedList2 = (function () { |
| 2 | + |
| 3 | + class Node { |
| 4 | + constructor(element, priority){ |
| 5 | + this.element = element; |
| 6 | + this.next = null; |
| 7 | + } |
| 8 | + } |
| 9 | + |
| 10 | + let length = 0; |
| 11 | + let head = null; |
| 12 | + |
| 13 | + class LinkedList2 { |
| 14 | + |
| 15 | + append(element) { |
| 16 | + |
| 17 | + let node = new Node(element), |
| 18 | + current; |
| 19 | + |
| 20 | + if (head === null) { //first node on list |
| 21 | + head = node; |
| 22 | + } else { |
| 23 | + |
| 24 | + current = head; |
| 25 | + |
| 26 | + //loop the list until find last item |
| 27 | + while (current.next) { |
| 28 | + current = current.next; |
| 29 | + } |
| 30 | + |
| 31 | + //get last item and assign next to added item to make the link |
| 32 | + current.next = node; |
| 33 | + } |
| 34 | + |
| 35 | + length++; //update size of list |
| 36 | + } |
| 37 | + |
| 38 | + insert(position, element) { |
| 39 | + |
| 40 | + //check for out-of-bounds values |
| 41 | + if (position >= 0 && position <= length) { |
| 42 | + |
| 43 | + let node = new Node(element), |
| 44 | + current = head, |
| 45 | + previous, |
| 46 | + index = 0; |
| 47 | + |
| 48 | + if (position === 0) { //add on first position |
| 49 | + |
| 50 | + node.next = current; |
| 51 | + head = node; |
| 52 | + |
| 53 | + } else { |
| 54 | + while (index++ < position) { |
| 55 | + previous = current; |
| 56 | + current = current.next; |
| 57 | + } |
| 58 | + node.next = current; |
| 59 | + previous.next = node; |
| 60 | + } |
| 61 | + |
| 62 | + length++; //update size of list |
| 63 | + |
| 64 | + return true; |
| 65 | + |
| 66 | + } else { |
| 67 | + return false; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + removeAt(position) { |
| 72 | + |
| 73 | + //check for out-of-bounds values |
| 74 | + if (position > -1 && position < length) { |
| 75 | + |
| 76 | + let current = head, |
| 77 | + previous, |
| 78 | + index = 0; |
| 79 | + |
| 80 | + //removing first item |
| 81 | + if (position === 0) { |
| 82 | + head = current.next; |
| 83 | + } else { |
| 84 | + |
| 85 | + while (index++ < position) { |
| 86 | + |
| 87 | + previous = current; |
| 88 | + current = current.next; |
| 89 | + } |
| 90 | + |
| 91 | + //link previous with current's next - skip it to remove |
| 92 | + previous.next = current.next; |
| 93 | + } |
| 94 | + |
| 95 | + length--; |
| 96 | + |
| 97 | + return current.element; |
| 98 | + |
| 99 | + } else { |
| 100 | + return null; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + remove(element) { |
| 105 | + |
| 106 | + let index = this.indexOf(element); |
| 107 | + return this.removeAt(index); |
| 108 | + } |
| 109 | + |
| 110 | + indexOf(element) { |
| 111 | + |
| 112 | + let current = head, |
| 113 | + index = 0; |
| 114 | + |
| 115 | + while (current) { |
| 116 | + if (element === current.element) { |
| 117 | + return index; |
| 118 | + } |
| 119 | + index++; |
| 120 | + current = current.next; |
| 121 | + } |
| 122 | + |
| 123 | + return -1; |
| 124 | + } |
| 125 | + |
| 126 | + isEmpty() { |
| 127 | + return length === 0; |
| 128 | + } |
| 129 | + |
| 130 | + size() { |
| 131 | + return length; |
| 132 | + } |
| 133 | + |
| 134 | + getHead() { |
| 135 | + return head; |
| 136 | + } |
| 137 | + |
| 138 | + toString() { |
| 139 | + |
| 140 | + let current = head, |
| 141 | + string = ''; |
| 142 | + |
| 143 | + while (current) { |
| 144 | + string += current.element + (current.next ? '\n' : ''); |
| 145 | + current = current.next; |
| 146 | + } |
| 147 | + return string; |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | + print() { |
| 152 | + console.log(this.toString()); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return LinkedList2; |
| 157 | +})(); |
0 commit comments