|
| 1 | +/** |
| 2 | + * A class that represents a node. |
| 3 | + * |
| 4 | + * <p>A node contains data of any type and a pointer to the next element. To be used in a linked |
| 5 | + * list implementation. |
| 6 | + * |
| 7 | + * @author Duong Nguyen |
| 8 | + */ |
| 9 | +class Node<T> { |
| 10 | + |
| 11 | + T data; |
| 12 | + Node next; |
| 13 | + |
| 14 | + /** |
| 15 | + * Constructs a new node in a list. |
| 16 | + * |
| 17 | + * @param data the data that this node contains |
| 18 | + * @param next the address of the next node in the list |
| 19 | + */ |
| 20 | + Node(T data, Node next) { |
| 21 | + this.data = data; |
| 22 | + this.next = next; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * An implementation of a singly linked list. |
| 28 | + * |
| 29 | + * <p>Implementation includes methods to insert and delete at the head, the tail, and a specified |
| 30 | + * index. |
| 31 | + * |
| 32 | + * @author Duong Nguyen |
| 33 | + */ |
| 34 | +@SuppressWarnings("unchecked") |
| 35 | +class LinkedList<T> { |
| 36 | + |
| 37 | + Node<T> head; |
| 38 | + Node<T> tail; |
| 39 | + |
| 40 | + /** Constructs a new linked list with a head node. */ |
| 41 | + LinkedList() { |
| 42 | + this.head = new Node<T>(null, null); |
| 43 | + this.tail = head; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns the length of this linked list. |
| 48 | + * |
| 49 | + * @return the length of this linked list |
| 50 | + */ |
| 51 | + int length() { |
| 52 | + Node node = this.head; |
| 53 | + int length = 0; |
| 54 | + |
| 55 | + while (node.next != null) { |
| 56 | + node = node.next; |
| 57 | + length++; |
| 58 | + } |
| 59 | + |
| 60 | + return length; |
| 61 | + } |
| 62 | + |
| 63 | + boolean isValidIndex(int index) { |
| 64 | + return index > -1 && index < this.length(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Returns the node at the specified index. |
| 69 | + * |
| 70 | + * @param index |
| 71 | + * @return the node at the specified index |
| 72 | + */ |
| 73 | + Node get(int index) { |
| 74 | + if (!this.isValidIndex(index)) return null; |
| 75 | + |
| 76 | + Node node = this.head.next; |
| 77 | + |
| 78 | + for (int i = 0; i < index; i++) |
| 79 | + if (node.next != null) node = node.next; |
| 80 | + else return null; |
| 81 | + |
| 82 | + return node; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Inserts a node with the specfied data at the beginning of this list. |
| 87 | + * |
| 88 | + * @param data the data that the node contains |
| 89 | + * @return the inserted node |
| 90 | + */ |
| 91 | + Node insertAtStart(T data) { |
| 92 | + Node node = new Node(data, this.head.next); |
| 93 | + this.head.next = node; |
| 94 | + |
| 95 | + return node; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Inserts a node with the specified data at the end of this list. |
| 100 | + * |
| 101 | + * @param data the data that the node contains |
| 102 | + * @return the inserted node |
| 103 | + */ |
| 104 | + Node insertAtEnd(T data) { |
| 105 | + Node node = new Node(data, null); |
| 106 | + this.tail.next = node; // old tail |
| 107 | + this.tail = node; // new tail |
| 108 | + |
| 109 | + return node; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Inserts a node with the specified data at the specified index. |
| 114 | + * |
| 115 | + * @param data the data that the node contains |
| 116 | + * @param index the index at which to insert the node |
| 117 | + * @return the inserted node |
| 118 | + */ |
| 119 | + Node insert(T data, int index) { |
| 120 | + if (!this.isValidIndex(index)) return null; |
| 121 | + |
| 122 | + Node node = new Node(data, this.get(index)); |
| 123 | + this.get(index - 1).next = node; |
| 124 | + |
| 125 | + return node; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Deletes the first node. |
| 130 | + * |
| 131 | + * @return the deleted node |
| 132 | + */ |
| 133 | + Node deleteAtStart() { |
| 134 | + if (this.head == this.tail) return null; |
| 135 | + |
| 136 | + Node node = this.head.next; |
| 137 | + this.head.next = node.next; |
| 138 | + |
| 139 | + return node; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Deletes the last node. |
| 144 | + * |
| 145 | + * @return the deleted node |
| 146 | + */ |
| 147 | + Node deleteAtEnd() { |
| 148 | + if (this.tail == this.head) return null; |
| 149 | + |
| 150 | + Node node = this.tail; |
| 151 | + Node newTail = this.get(this.length() - 2); |
| 152 | + newTail.next = null; |
| 153 | + this.tail = newTail; |
| 154 | + |
| 155 | + return node; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Deletes the node at the specified index. |
| 160 | + * |
| 161 | + * @param index the index of the node to be deleted |
| 162 | + * @return the deleted node |
| 163 | + */ |
| 164 | + Node delete(int index) { |
| 165 | + if (!this.isValidIndex(index)) return null; |
| 166 | + if (index == 0) return this.deleteAtStart(); |
| 167 | + |
| 168 | + Node prevNode = this.get(index - 1); |
| 169 | + Node node = prevNode.next; |
| 170 | + prevNode.next = node.next; |
| 171 | + |
| 172 | + return node; |
| 173 | + } |
| 174 | +} |
0 commit comments