|
| 1 | +public class CursorLinkedList<T> { |
| 2 | + |
| 3 | + private static class Node<T> { |
| 4 | + |
| 5 | + T element; |
| 6 | + int next; |
| 7 | + |
| 8 | + Node(T element, int next) { |
| 9 | + this.element = element; |
| 10 | + this.next = next; |
| 11 | + } |
| 12 | + |
| 13 | + boolean isEmpty() { |
| 14 | + return element == null; |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + |
| 19 | + private final int os; |
| 20 | + private int head; |
| 21 | + private final Node<T>[] cursorSpace; |
| 22 | + private int count; |
| 23 | + private final static int CURSOR_SPACE_SIZE = 100; |
| 24 | + |
| 25 | + |
| 26 | + { |
| 27 | + // init at loading time |
| 28 | + cursorSpace = new Node[CURSOR_SPACE_SIZE]; |
| 29 | + for (int i = 0; i < CURSOR_SPACE_SIZE; i++) { |
| 30 | + cursorSpace[i] = new Node<>(null, i + 1); |
| 31 | + } |
| 32 | + cursorSpace[CURSOR_SPACE_SIZE - 1].next = 0; |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | + public CursorLinkedList() { |
| 37 | + os = 0; |
| 38 | + count = 0; |
| 39 | + head = -1; |
| 40 | + } |
| 41 | + |
| 42 | + public void printList() { |
| 43 | + |
| 44 | + if (head != -1) { |
| 45 | + |
| 46 | + |
| 47 | + int start = head; |
| 48 | + while (start != -1) { |
| 49 | + |
| 50 | + T element = cursorSpace[start].element; |
| 51 | + System.out.println(element.toString()); |
| 52 | + start = cursorSpace[start].next; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + /** |
| 60 | + * @return the logical index of the element within the list , not the actual |
| 61 | + * index of the [cursorSpace] array |
| 62 | + */ |
| 63 | + public int indexOf(T element) { |
| 64 | + |
| 65 | + |
| 66 | + Objects.requireNonNull(element); |
| 67 | + Node<T> iterator = cursorSpace[head]; |
| 68 | + for (int i = 0; i < count; i++) { |
| 69 | + if (iterator.element.equals(element)) { |
| 70 | + return i; |
| 71 | + } |
| 72 | + iterator = cursorSpace[iterator.next]; |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + return -1; |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + /** |
| 81 | + * @param position , the logical index of the element , not the actual one |
| 82 | + * within the [cursorSpace] array . |
| 83 | + * this method should be used to get the index give by indexOf() method. |
| 84 | + * @return |
| 85 | + */ |
| 86 | + |
| 87 | + public T get(int position) { |
| 88 | + |
| 89 | + if (position >= 0 && position < count) { |
| 90 | + |
| 91 | + int start = head; |
| 92 | + int counter = 0; |
| 93 | + while (start != -1) { |
| 94 | + |
| 95 | + T element = cursorSpace[start].element; |
| 96 | + if (counter == position){ |
| 97 | + return element; |
| 98 | + } |
| 99 | + |
| 100 | + start = cursorSpace[start].next; |
| 101 | + counter++; |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + public void removeByIndex(int index){ |
| 111 | + |
| 112 | + if(index >= 0 && index < count){ |
| 113 | + |
| 114 | + T element = get(index); |
| 115 | + remove(element); |
| 116 | + } |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + public void remove(T element) { |
| 121 | + |
| 122 | + |
| 123 | + Objects.requireNonNull(element); |
| 124 | + |
| 125 | + // case element is in the head |
| 126 | + T temp_element = cursorSpace[head].element; |
| 127 | + int temp_next = cursorSpace[head].next; |
| 128 | + if (temp_element.equals(element)) { |
| 129 | + free(head); |
| 130 | + head = temp_next; |
| 131 | + } else { // otherwise cases |
| 132 | + |
| 133 | + int prev_index = head; |
| 134 | + int current_index = cursorSpace[prev_index].next; |
| 135 | + |
| 136 | + while (current_index != -1 ) { |
| 137 | + |
| 138 | + T current_element = cursorSpace[current_index].element; |
| 139 | + if(current_element.equals(element)){ |
| 140 | + cursorSpace[prev_index].next = cursorSpace[current_index].next; |
| 141 | + free(current_index); |
| 142 | + break; |
| 143 | + } |
| 144 | + |
| 145 | + prev_index = current_index; |
| 146 | + current_index = cursorSpace[prev_index].next; |
| 147 | + } |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | + count--; |
| 153 | + |
| 154 | + } |
| 155 | + |
| 156 | + private void free(int index) { |
| 157 | + |
| 158 | + Node os_node = cursorSpace[os]; |
| 159 | + int os_next = os_node.next; |
| 160 | + cursorSpace[os].next = index; |
| 161 | + cursorSpace[index].element = null; |
| 162 | + cursorSpace[index].next = os_next; |
| 163 | + |
| 164 | + } |
| 165 | + |
| 166 | + |
| 167 | + public void append(T element) { |
| 168 | + |
| 169 | + Objects.requireNonNull(element); |
| 170 | + int availableIndex = alloc(); |
| 171 | + cursorSpace[availableIndex].element = element; |
| 172 | + |
| 173 | + if (head == -1) { |
| 174 | + head = availableIndex; |
| 175 | + } |
| 176 | + |
| 177 | + int iterator = head; |
| 178 | + while (cursorSpace[iterator].next != -1) { |
| 179 | + iterator = cursorSpace[iterator].next; |
| 180 | + } |
| 181 | + |
| 182 | + cursorSpace[iterator].next = availableIndex; |
| 183 | + cursorSpace[availableIndex].next = -1; |
| 184 | + |
| 185 | + |
| 186 | + count++; |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * @return the index of the next available node |
| 191 | + */ |
| 192 | + private int alloc() { |
| 193 | + |
| 194 | + |
| 195 | + //1- get the index at which the os is pointing |
| 196 | + int availableNodeIndex = cursorSpace[os].next; |
| 197 | + |
| 198 | + if (availableNodeIndex == 0) { |
| 199 | + throw new OutOfMemoryError(); |
| 200 | + } |
| 201 | + |
| 202 | + //2- make the os point to the next of the @var{availableNodeIndex} |
| 203 | + int availableNext = cursorSpace[availableNodeIndex].next; |
| 204 | + cursorSpace[os].next = availableNext; |
| 205 | + |
| 206 | + // this to indicate an end of the list , helpful at testing since any err |
| 207 | + // would throw an outOfBoundException |
| 208 | + cursorSpace[availableNodeIndex].next = -1; |
| 209 | + |
| 210 | + return availableNodeIndex; |
| 211 | + |
| 212 | + } |
| 213 | + |
| 214 | + |
| 215 | +} |
0 commit comments