|
| 1 | +// Node structure |
| 2 | +class Node { |
| 3 | + constructor(value) { |
| 4 | + this.value = value; |
| 5 | + this.next = null; |
| 6 | + } |
| 7 | +} |
| 8 | + |
| 9 | +// Linked List class |
| 10 | +class LinkedList { |
| 11 | + constructor() { |
| 12 | + this.head = null; |
| 13 | + this.length = 0; |
| 14 | + } |
| 15 | + |
| 16 | + // Add to end |
| 17 | + append(value) { |
| 18 | + const newNode = new Node(value); |
| 19 | + if (!this.head) { |
| 20 | + this.head = newNode; |
| 21 | + } else { |
| 22 | + let current = this.head; |
| 23 | + while (current.next) { |
| 24 | + current = current.next; |
| 25 | + } |
| 26 | + current.next = newNode; |
| 27 | + } |
| 28 | + this.length++; |
| 29 | + } |
| 30 | + |
| 31 | + // Add to beginning |
| 32 | + prepend(value) { |
| 33 | + const newNode = new Node(value); |
| 34 | + newNode.next = this.head; |
| 35 | + this.head = newNode; |
| 36 | + this.length++; |
| 37 | + } |
| 38 | + |
| 39 | + // Insert at index |
| 40 | + insertAt(index, value) { |
| 41 | + if (index < 0 || index > this.length) return console.log("Invalid index"); |
| 42 | + if (index === 0) return this.prepend(value); |
| 43 | + if (index === this.length) return this.append(value); |
| 44 | + |
| 45 | + const newNode = new Node(value); |
| 46 | + let current = this.head; |
| 47 | + let prev = null; |
| 48 | + let i = 0; |
| 49 | + while (i < index) { |
| 50 | + prev = current; |
| 51 | + current = current.next; |
| 52 | + i++; |
| 53 | + } |
| 54 | + newNode.next = current; |
| 55 | + prev.next = newNode; |
| 56 | + this.length++; |
| 57 | + } |
| 58 | + |
| 59 | + // Remove by value |
| 60 | + remove(value) { |
| 61 | + if (!this.head) return; |
| 62 | + |
| 63 | + if (this.head.value === value) { |
| 64 | + this.head = this.head.next; |
| 65 | + this.length--; |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + let current = this.head; |
| 70 | + let prev = null; |
| 71 | + while (current && current.value !== value) { |
| 72 | + prev = current; |
| 73 | + current = current.next; |
| 74 | + } |
| 75 | + |
| 76 | + if (current) { |
| 77 | + prev.next = current.next; |
| 78 | + this.length--; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Remove at index |
| 83 | + removeAt(index) { |
| 84 | + if (index < 0 || index >= this.length) return console.log("Invalid index"); |
| 85 | + |
| 86 | + if (index === 0) { |
| 87 | + this.head = this.head.next; |
| 88 | + } else { |
| 89 | + let current = this.head; |
| 90 | + let prev = null; |
| 91 | + let i = 0; |
| 92 | + while (i < index) { |
| 93 | + prev = current; |
| 94 | + current = current.next; |
| 95 | + i++; |
| 96 | + } |
| 97 | + prev.next = current.next; |
| 98 | + } |
| 99 | + this.length--; |
| 100 | + } |
| 101 | + |
| 102 | + // Search for value |
| 103 | + search(value) { |
| 104 | + let current = this.head; |
| 105 | + let index = 0; |
| 106 | + while (current) { |
| 107 | + if (current.value === value) return index; |
| 108 | + current = current.next; |
| 109 | + index++; |
| 110 | + } |
| 111 | + return -1; // Not found |
| 112 | + } |
| 113 | + |
| 114 | + // Print all nodes |
| 115 | + printList() { |
| 116 | + let current = this.head; |
| 117 | + let result = ""; |
| 118 | + while (current) { |
| 119 | + result += current.value + " -> "; |
| 120 | + current = current.next; |
| 121 | + } |
| 122 | + console.log(result + "null"); |
| 123 | + } |
| 124 | + |
| 125 | + // Get size |
| 126 | + size() { |
| 127 | + return this.length; |
| 128 | + } |
| 129 | + |
| 130 | + // Check if empty |
| 131 | + isEmpty() { |
| 132 | + return this.length === 0; |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// Example usage |
| 137 | +const list = new LinkedList(); |
| 138 | +list.append(10); |
| 139 | +list.append(20); |
| 140 | +list.prepend(5); |
| 141 | +list.insertAt(1, 15); |
| 142 | +list.printList(); // Output: 5 -> 15 -> 10 -> 20 -> null |
| 143 | +list.remove(15); |
| 144 | +list.printList(); // Output: 5 -> 10 -> 20 -> null |
| 145 | +console.log(list.search(10)); // Output: 1 |
| 146 | +console.log(list.size()); // Output: 3 |
| 147 | +console.log(list.isEmpty()); // Output: false |
0 commit comments