| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | export class Element { |
| 7 | | constructor() { |
| 8 | | throw new Error('Remove this line and implement the function'); |
| 7 | constructor(value) { |
| 8 | this._value = value; |
| 9 | this._next = null; |
| 9 | 10 | } |
| 10 | 11 | |
| 11 | 12 | get value() { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 13 | return this._value; |
| 13 | 14 | } |
| 14 | 15 | |
| 15 | 16 | get next() { |
| 16 | | throw new Error('Remove this line and implement the function'); |
| 17 | return this._next; |
| 17 | 18 | } |
| 19 | |
| 20 | set next(next) { |
| 21 | this._next = next; |
| 22 | } |
| 23 | |
| 24 | // Handled Edge Cases: none for this class |
| 18 | 25 | } |
| 19 | 26 | |
| 20 | 27 | export class List { |
| 21 | | constructor() { |
| 22 | | throw new Error('Remove this line and implement the function'); |
| 28 | constructor(values = []) { |
| 29 | this._head = null; |
| 30 | this._length = 0; |
| 31 | |
| 32 | // Edge Case: Handle initialization with array of values |
| 33 | if (Array.isArray(values) && values.length > 0) { |
| 34 | for (const value of values) { |
| 35 | this.add(new Element(value)); |
| 36 | } |
| 37 | } |
| 23 | 38 | } |
| 24 | 39 | |
| 25 | | add(nextValue) { |
| 26 | | throw new Error('Remove this line and implement the function'); |
| 40 | add(element) { |
| 41 | // Edge Case: Handle adding to empty list |
| 42 | if (this._head === null) { |
| 43 | this._head = element; |
| 44 | } else { |
| 45 | // Edge Case: Handle adding to non-empty list |
| 46 | element.next = this._head; |
| 47 | this._head = element; |
| 48 | } |
| 49 | this._length++; |
| 27 | 50 | } |
| 28 | 51 | |
| 29 | 52 | get length() { |
| 30 | | throw new Error('Remove this line and implement the function'); |
| 53 | return this._length; |
| 31 | 54 | } |
| 32 | 55 | |
| 33 | 56 | get head() { |
| 34 | | throw new Error('Remove this line and implement the function'); |
| 57 | return this._head; |
| 35 | 58 | } |
| 36 | 59 | |
| 37 | 60 | toArray() { |
| 38 | | throw new Error('Remove this line and implement the function'); |
| 61 | const result = []; |
| 62 | let current = this._head; |
| 63 | |
| 64 | // Edge Case: Handle empty list |
| 65 | if (current === null) { |
| 66 | return result; |
| 67 | } |
| 68 | |
| 69 | // Edge Case: Handle single element list |
| 70 | while (current !== null) { |
| 71 | result.push(current.value); |
| 72 | current = current.next; |
| 73 | } |
| 74 | |
| 75 | return result; |
| 39 | 76 | } |
| 40 | 77 | |
| 41 | 78 | reverse() { |
| 42 | | throw new Error('Remove this line and implement the function'); |
| 79 | const reversedList = new List(); |
| 80 | let current = this._head; |
| 81 | |
| 82 | // Edge Case: Handle empty list |
| 83 | if (current === null) { |
| 84 | return reversedList; |
| 85 | } |
| 86 | |
| 87 | // Edge Case: Handle single element list |
| 88 | while (current !== null) { |
| 89 | reversedList.add(new Element(current.value)); |
| 90 | current = current.next; |
| 91 | } |
| 92 | |
| 93 | return reversedList; |
| 94 | } |
| 95 | |
| 96 | // Handled Edge Cases: empty list initialization, array initialization, adding to empty list, adding to non-empty list, traversing empty list, traversing single element list, reversing empty list, reversing single element list |
| 97 | |
| 98 | // Create a linked list from a range of song IDs (for music player prototype) |
| 99 | static fromRange(start, end) { |
| 100 | const list = new List(); |
| 101 | |
| 102 | // Edge Case: Handle invalid range |
| 103 | if (start > end) { |
| 104 | return list; // Return empty list for invalid range |
| 105 | } |
| 106 | |
| 107 | // Edge Case: Handle empty range |
| 108 | if (start === end) { |
| 109 | list.add(new Element(start)); |
| 110 | return list; |
| 111 | } |
| 112 | |
| 113 | // Create elements from range (start to end inclusive) |
| 114 | for (let i = end; i >= start; i--) { |
| 115 | list.add(new Element(i)); |
| 116 | } |
| 117 | |
| 118 | return list; |
| 43 | 119 | } |
| 44 | 120 | } |