|
| 1 | +## Purpose |
| 2 | + |
| 3 | +I've never implemented a `DoublyLinkedList` and thought it would be a good data structure exercise. I tried using outside |
| 4 | +resources as little as possible. I also tried to keep things as simple as possible |
| 5 | + |
| 6 | +## Discussion |
| 7 | + |
| 8 | +The `DoublyLinkedList` is made up of a `Node` `head`, a `Node` `tail`, and an `int` `length`. I did not genericize the |
| 9 | +API to keep things simple - the API only deals with `int` values. |
| 10 | + |
| 11 | +The API is |
| 12 | + |
| 13 | +* `getHead` - gets the `Node` at the `head` of the linked list in `O(1)` |
| 14 | +* `getTail` - gets the `Node` at the `tail` of the linked list in `O(1)` |
| 15 | +* `getLength` - gets the `length` of the linked list in `O(1)` |
| 16 | +* `isEmpty` - convenience method; returns `true` if `length` is `0` and `false` otherwise in `O(1)` |
| 17 | +* `insertAtHead` - adds the input value at the `head` of the linked list in `O(1)` |
| 18 | +* `insertAtTail` - adds the input value at the `tail` of the linked list in `O(1)` |
| 19 | +* `insertAt` - adds the input value at some index in the linked list in `O(n)` |
| 20 | +* `removeFirstOccurrence` - removes the first occurrence of the input value (if it exists in list) in `O(n)` |
| 21 | +* `removeAt` - removes the node at the specified index in the linked list in `O(n)` |
| 22 | +* `removeHead` - removes the node at the `head` of the linked list in `O(1)` |
| 23 | +* `removeTail` - removes the node at the `tail` of the linked list in `O(1)` |
| 24 | + |
| 25 | +Things I can improve: |
| 26 | + |
| 27 | +* When inserting at / removing at a particular index, I could speed up execution time by picking to start |
| 28 | + at the beginning or end of the list based on which the index is closest to |
| 29 | +* I tried adding a bunch of test cases, but I could have easily missed a case |
| 30 | +* Is my API sane / reasonable / did I miss implementing any obvious methods? |
| 31 | + |
| 32 | +## Implementation |
| 33 | + |
| 34 | +<!-- language:lang-java --!> |
| 35 | + |
| 36 | + public class DoublyLinkedList { |
| 37 | + public static class Node { |
| 38 | + private Node previous; |
| 39 | + private Node next; |
| 40 | + private int value; |
| 41 | + |
| 42 | + public Node(int value) { |
| 43 | + this.value = value; |
| 44 | + } |
| 45 | + |
| 46 | + public Node getPrevious() { |
| 47 | + return previous; |
| 48 | + } |
| 49 | + |
| 50 | + public Node getNext() { |
| 51 | + return next; |
| 52 | + } |
| 53 | + |
| 54 | + public int getValue() { |
| 55 | + return value; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private Node head; |
| 60 | + private Node tail; |
| 61 | + private int length; |
| 62 | + |
| 63 | + public DoublyLinkedList() { |
| 64 | + } |
| 65 | + |
| 66 | + public Node getHead() { |
| 67 | + return head; |
| 68 | + } |
| 69 | + |
| 70 | + public Node getTail() { |
| 71 | + return tail; |
| 72 | + } |
| 73 | + |
| 74 | + public int getLength() { |
| 75 | + return length; |
| 76 | + } |
| 77 | + |
| 78 | + public boolean isEmpty() { |
| 79 | + return length == 0; |
| 80 | + } |
| 81 | + |
| 82 | + public void insertAtHead(int value) { |
| 83 | + insertNode(null, new Node(value), head); |
| 84 | + } |
| 85 | + |
| 86 | + public void insertAtTail(int value) { |
| 87 | + insertNode(tail, new Node(value), null); |
| 88 | + } |
| 89 | + |
| 90 | + public void insertAt(int value, int index) { |
| 91 | + if (index > length || index < 0) { |
| 92 | + throw new IndexOutOfBoundsException("index of " + index + " is out of bounds"); |
| 93 | + } |
| 94 | + |
| 95 | + Node node = new Node(value); |
| 96 | + Node previousNode = null; |
| 97 | + Node currentNodeAtIndex = head; |
| 98 | + for (int i = 0; i < index; i++) { |
| 99 | + previousNode = currentNodeAtIndex; |
| 100 | + currentNodeAtIndex = currentNodeAtIndex.next; |
| 101 | + } |
| 102 | + insertNode(previousNode, node, currentNodeAtIndex); |
| 103 | + } |
| 104 | + |
| 105 | + public void removeFirstOccurrence(int value) { |
| 106 | + Node currentNode = head; |
| 107 | + while (currentNode != null) { |
| 108 | + if (currentNode.value == value) { |
| 109 | + removeNode(currentNode); |
| 110 | + return; |
| 111 | + } |
| 112 | + currentNode = currentNode.next; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public void removeAt(int index) { |
| 117 | + if (index >= length || index < 0) { |
| 118 | + throw new IndexOutOfBoundsException("index of " + index + " is out of bounds"); |
| 119 | + } |
| 120 | + |
| 121 | + Node currentNodeAtIndex = head; |
| 122 | + for (int i = 0; i < index; i++) { |
| 123 | + currentNodeAtIndex = currentNodeAtIndex.next; |
| 124 | + } |
| 125 | + |
| 126 | + removeNode(currentNodeAtIndex); |
| 127 | + } |
| 128 | + |
| 129 | + public void removeHead() { |
| 130 | + removeNode(head); |
| 131 | + } |
| 132 | + |
| 133 | + public void removeTail() { |
| 134 | + removeNode(tail); |
| 135 | + } |
| 136 | + |
| 137 | + private void removeNode(Node node) { |
| 138 | + if (node.previous == null) { |
| 139 | + head = node.next; |
| 140 | + } |
| 141 | + |
| 142 | + if (node.next == null) { |
| 143 | + tail = node.previous; |
| 144 | + } |
| 145 | + |
| 146 | + if (node.previous != null) { |
| 147 | + node.previous.next = node.next; |
| 148 | + } |
| 149 | + |
| 150 | + if (node.next != null) { |
| 151 | + node.next.previous = node.previous; |
| 152 | + } |
| 153 | + |
| 154 | + length--; |
| 155 | + } |
| 156 | + |
| 157 | + private void insertNode(Node nodeBefore, Node node, Node nodeAfter) { |
| 158 | + node.next = nodeAfter; |
| 159 | + node.previous = nodeBefore; |
| 160 | + |
| 161 | + if (nodeBefore == null) { |
| 162 | + head = node; |
| 163 | + } |
| 164 | + |
| 165 | + if (nodeAfter == null) { |
| 166 | + tail = node; |
| 167 | + } |
| 168 | + |
| 169 | + if (nodeBefore != null) { |
| 170 | + nodeBefore.next = node; |
| 171 | + } |
| 172 | + |
| 173 | + if (nodeAfter != null) { |
| 174 | + nodeAfter.previous = node; |
| 175 | + } |
| 176 | + |
| 177 | + length++; |
| 178 | + } |
| 179 | + } |
0 commit comments