|
| 1 | +export interface INode<T> { |
| 2 | + data: T; |
| 3 | + next?: INode<T>; |
| 4 | +} |
| 5 | + |
| 6 | +export class Node<T> implements INode<T> { |
| 7 | + public data: T; |
| 8 | + public next?: INode<T>; |
| 9 | + |
| 10 | + constructor(data: T) { |
| 11 | + this.data = data; |
| 12 | + this.next = null; |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +export class LinkedList<T> { |
| 17 | + private head: INode<T> | null; |
| 18 | + protected tail: INode<T> | null; |
| 19 | + protected length: number; |
| 20 | + |
| 21 | + constructor(data: T) { |
| 22 | + this.head = new Node(data); |
| 23 | + this.tail = this.head; |
| 24 | + this.length = 1; |
| 25 | + } |
| 26 | + |
| 27 | + public append(data: T): LinkedList<T> { |
| 28 | + // Create a new node |
| 29 | + const newNode = new Node(data); |
| 30 | + // Attach it after the first node |
| 31 | + this.tail.next = newNode; |
| 32 | + // Update the tail to point the newNode |
| 33 | + this.tail = newNode; |
| 34 | + // Increment length |
| 35 | + this.length++; |
| 36 | + // Return the new created list |
| 37 | + return this; |
| 38 | + } |
| 39 | + |
| 40 | + public prepend(data: T): LinkedList<T> { |
| 41 | + // Create a new node |
| 42 | + const newNode = new Node(data); |
| 43 | + // Create a pointer to the head of the list |
| 44 | + newNode.next = this.head; |
| 45 | + // Update head reference to the newNode |
| 46 | + this.head = newNode; |
| 47 | + // Increment length |
| 48 | + this.length++; |
| 49 | + // Return the new created list |
| 50 | + return this; |
| 51 | + } |
| 52 | + |
| 53 | + // Prints linked list in array format for better readability |
| 54 | + public printList(): Array<T> { |
| 55 | + const array: T[] = []; |
| 56 | + let currentNode = this.head; |
| 57 | + while (currentNode !== null) { |
| 58 | + array.push(currentNode.data); |
| 59 | + currentNode = currentNode.next; |
| 60 | + } |
| 61 | + return array; |
| 62 | + } |
| 63 | + |
| 64 | + public insert(index: number, data: T) { |
| 65 | + // Check params |
| 66 | + if (index >= this.length) { |
| 67 | + return this.append(data); |
| 68 | + } |
| 69 | + // Create a new node |
| 70 | + const newNode = new Node(data); |
| 71 | + // Get the index of previous node |
| 72 | + const prevNode = this.traverse(index - 1); |
| 73 | + // Temporarily hold the pointer to previous node |
| 74 | + const nextNode = prevNode.next; |
| 75 | + // Update previous node with the new one |
| 76 | + prevNode.next = newNode; |
| 77 | + // Point the new node to the next node |
| 78 | + newNode.next = nextNode; |
| 79 | + // Increment length |
| 80 | + this.length++; |
| 81 | + // Print list to the console |
| 82 | + return this.printList(); |
| 83 | + } |
| 84 | + |
| 85 | + public remove(index: number) { |
| 86 | + // Get the index of previous node |
| 87 | + const prevNode = this.traverse(index - 1); |
| 88 | + // Reference node to be removed |
| 89 | + const currentNode = prevNode.next; |
| 90 | + // Reference previous node with the next after the removed one |
| 91 | + prevNode.next = currentNode.next; |
| 92 | + // Decrease length |
| 93 | + this.length--; |
| 94 | + // Print list to the console |
| 95 | + return this.printList(); |
| 96 | + } |
| 97 | + |
| 98 | + private traverse(index: number): INode<T> { |
| 99 | + // Check params |
| 100 | + let counter = 0; |
| 101 | + let currentNode = this.head; |
| 102 | + while (counter !== index) { |
| 103 | + currentNode = currentNode.next; |
| 104 | + counter++; |
| 105 | + } |
| 106 | + return currentNode; |
| 107 | + } |
| 108 | +} |
0 commit comments