|
| 1 | +package section16_Hashmap; |
| 2 | + |
| 3 | +public class GenericLinkedList<T> { |
| 4 | + |
| 5 | + private class Node { |
| 6 | + private T data; |
| 7 | + private Node next; |
| 8 | + } |
| 9 | + |
| 10 | + private Node head; |
| 11 | + private Node tail; |
| 12 | + private int size; |
| 13 | + |
| 14 | + // O(N) Time |
| 15 | + public void display() { |
| 16 | + System.out.println("-----------------------------------------------"); |
| 17 | + Node temp = this.head; |
| 18 | + |
| 19 | + while (temp != null) { |
| 20 | + System.out.print(temp.data + " -> "); |
| 21 | + temp = temp.next; |
| 22 | + } |
| 23 | + System.out.println("NULL"); |
| 24 | + |
| 25 | + } |
| 26 | + |
| 27 | + // O(1) Time |
| 28 | + public void addLast(T item) { |
| 29 | + // create a new node |
| 30 | + Node newNode = new Node(); |
| 31 | + newNode.data = item; |
| 32 | + newNode.next = null; |
| 33 | + |
| 34 | + if (this.size >= 1) |
| 35 | + this.tail.next = newNode; |
| 36 | + |
| 37 | + if (this.size == 0) { |
| 38 | + this.head = newNode; |
| 39 | + this.tail = newNode; |
| 40 | + } else { |
| 41 | + this.tail = newNode; |
| 42 | + } |
| 43 | + |
| 44 | + this.size++; |
| 45 | + } |
| 46 | + |
| 47 | + // O(1) Time |
| 48 | + public void addFirst(T item) { |
| 49 | + Node newNode = new Node(); |
| 50 | + newNode.data = item; |
| 51 | + newNode.next = null; |
| 52 | + |
| 53 | + if (this.size == 0) { |
| 54 | + this.head = newNode; |
| 55 | + this.tail = newNode; |
| 56 | + } else { |
| 57 | + newNode.next = this.head; |
| 58 | + this.head = newNode; |
| 59 | + } |
| 60 | + this.size++; |
| 61 | + } |
| 62 | + |
| 63 | + // O(1) Time |
| 64 | + public T getFirst() throws Exception { |
| 65 | + if (this.size == 0) |
| 66 | + throw new Exception("LinkedList is empty"); |
| 67 | + |
| 68 | + return this.head.data; |
| 69 | + } |
| 70 | + |
| 71 | + // O(1) Time |
| 72 | + public T getLast() throws Exception { |
| 73 | + if (this.size == 0) |
| 74 | + throw new Exception("LinkedList is empty"); |
| 75 | + |
| 76 | + return this.tail.data; |
| 77 | + } |
| 78 | + |
| 79 | + // O(N) Time |
| 80 | + public T getAt(int index) throws Exception { |
| 81 | + if (this.size == 0) |
| 82 | + throw new Exception("LinkedList is empty..."); |
| 83 | + |
| 84 | + if (index < 0 || index >= this.size) { |
| 85 | + throw new Exception("invalid index..."); |
| 86 | + } |
| 87 | + |
| 88 | + Node temp = this.head; |
| 89 | + int id = 0; |
| 90 | + while (id < index) { |
| 91 | + temp = temp.next; |
| 92 | + id++; |
| 93 | + } |
| 94 | + return temp.data; |
| 95 | + } |
| 96 | + |
| 97 | + // O(N) Time |
| 98 | + public Node getNodeAt(int index) throws Exception { |
| 99 | + if (this.size == 0) |
| 100 | + throw new Exception("LinkedList is empty..."); |
| 101 | + |
| 102 | + if (index < 0 || index >= this.size) { |
| 103 | + throw new Exception("invalid index..."); |
| 104 | + } |
| 105 | + |
| 106 | + Node temp = this.head; |
| 107 | + int id = 0; |
| 108 | + while (id < index) { |
| 109 | + temp = temp.next; |
| 110 | + id++; |
| 111 | + } |
| 112 | + return temp; |
| 113 | + } |
| 114 | + |
| 115 | + // O(N) Time |
| 116 | + public void addAt(int index, T item) throws Exception { |
| 117 | + if (index < 0 || index > this.size) { |
| 118 | + throw new Exception("invalid index..."); |
| 119 | + } |
| 120 | + |
| 121 | + if (index == 0 || this.size == 0) { |
| 122 | + this.addFirst(item); |
| 123 | + return; |
| 124 | + } |
| 125 | + if (index == this.size) { |
| 126 | + this.addLast(item); |
| 127 | + return; |
| 128 | + } |
| 129 | + Node nn = new Node(); |
| 130 | + nn.data = item; |
| 131 | + nn.next = null; |
| 132 | + |
| 133 | + Node temp = this.head; |
| 134 | + int id = 1; |
| 135 | + while (id < index) { |
| 136 | + temp = temp.next; |
| 137 | + id++; |
| 138 | + } |
| 139 | + // can also get above using getNodeAt(index-1) |
| 140 | + |
| 141 | + nn.next = temp.next; |
| 142 | + temp.next = nn; |
| 143 | + this.size++; |
| 144 | + } |
| 145 | + |
| 146 | + // O(1) Time |
| 147 | + public T removeFirst() throws Exception { |
| 148 | + if (this.size == 0) |
| 149 | + throw new Exception("LinkedList is empty"); |
| 150 | + |
| 151 | + T firstNodeItem = this.head.data; |
| 152 | + |
| 153 | + if (this.size == 1) { |
| 154 | + this.head = null; |
| 155 | + this.tail = null; |
| 156 | + this.size--; |
| 157 | + } else { |
| 158 | + this.head = this.head.next; |
| 159 | + this.size--; |
| 160 | + } |
| 161 | + return firstNodeItem; |
| 162 | + } |
| 163 | + |
| 164 | + // O(N) Time |
| 165 | + public T removeLast() throws Exception { |
| 166 | + if (this.size == 0) |
| 167 | + throw new Exception("LinkedList is empty"); |
| 168 | + |
| 169 | + T lastNodeItem = this.tail.data; |
| 170 | + |
| 171 | + if (this.size == 1) { |
| 172 | + this.head = null; |
| 173 | + this.tail = null; |
| 174 | + this.size--; |
| 175 | + } else { |
| 176 | + Node secondLast = this.getNodeAt(this.size - 2); |
| 177 | + secondLast.next = null; |
| 178 | + this.tail = secondLast; |
| 179 | + this.size--; |
| 180 | + } |
| 181 | + return lastNodeItem; |
| 182 | + } |
| 183 | + |
| 184 | + // O(N) Time |
| 185 | + public T removeAt(int index) throws Exception { |
| 186 | + if (this.size == 0) |
| 187 | + throw new Exception("LinkedList is empty"); |
| 188 | + |
| 189 | + if (index < 0 || index >= this.size) |
| 190 | + throw new Exception("invalid index"); |
| 191 | + |
| 192 | + if (index == 0) |
| 193 | + return this.removeFirst(); |
| 194 | + else if (index == this.size - 1) |
| 195 | + return this.removeLast(); |
| 196 | + else { |
| 197 | + Node nodePrev = this.getNodeAt(index - 1); |
| 198 | + T returnVal = nodePrev.next.data; |
| 199 | + |
| 200 | + nodePrev.next = nodePrev.next.next; |
| 201 | + |
| 202 | + this.size--; |
| 203 | + |
| 204 | + return returnVal; |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + public int find(T value) { |
| 209 | + int index = 0; |
| 210 | + for (Node temp = this.head; temp != null; temp = temp.next) { |
| 211 | + if (temp.data.equals(value)) |
| 212 | + return index; |
| 213 | + index++; |
| 214 | + } |
| 215 | + return -1; |
| 216 | + } |
| 217 | + |
| 218 | +} |
0 commit comments