|
| 1 | +package org.pg4200.sol02; |
| 2 | + |
| 3 | +import org.pg4200.les02.list.MyList; |
| 4 | + |
| 5 | +/** |
| 6 | + * Created by arcuri82 on 05-Jun-19. |
| 7 | + */ |
| 8 | +public class MyMiddleBidirectionalLinkedList<T> implements MyList<T> { |
| 9 | + |
| 10 | + /* |
| 11 | + Is this better than MyBidirectionalLinkedList? |
| 12 | + As usual, it all depends... |
| 13 | + The get(i) in the 25%-75% range will be faster, but each add/delete becomes |
| 14 | + more expensive, as need to update the middle link |
| 15 | + */ |
| 16 | + |
| 17 | + private class ListNode{ |
| 18 | + T value; |
| 19 | + ListNode next; |
| 20 | + ListNode previous; |
| 21 | + } |
| 22 | + |
| 23 | + private ListNode head; |
| 24 | + private ListNode tail; |
| 25 | + private ListNode middle; |
| 26 | + private int size; |
| 27 | + |
| 28 | + |
| 29 | + private int expectedIndexOfMiddle(){ |
| 30 | + if(isEmpty()){ |
| 31 | + return -1; |
| 32 | + } |
| 33 | + |
| 34 | + /* |
| 35 | + we do not keep track of the index of middle, as that always |
| 36 | + can be inferred by the size of the list |
| 37 | + */ |
| 38 | + return (int) (Math.ceil(size() / 2.0) - 1); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void delete(int index) { |
| 43 | + |
| 44 | + if(index < 0 || index >= size()){ |
| 45 | + throw new IndexOutOfBoundsException(); |
| 46 | + } |
| 47 | + |
| 48 | + if(index == 0){ |
| 49 | + if(head.next != null){ |
| 50 | + head = head.next; |
| 51 | + head.previous = null; |
| 52 | + } else { |
| 53 | + head = null; |
| 54 | + tail = null; |
| 55 | + } |
| 56 | + } else if(index == (size-1)){ |
| 57 | + |
| 58 | + tail.previous.next = null; |
| 59 | + tail = tail.previous; |
| 60 | + |
| 61 | + } else { |
| 62 | + |
| 63 | + ListNode target = getNode(index); |
| 64 | + |
| 65 | + target.previous.next = target.next; |
| 66 | + target.next.previous = target.previous; |
| 67 | + } |
| 68 | + |
| 69 | + int previous = expectedIndexOfMiddle(); |
| 70 | + if(index <= previous){ |
| 71 | + previous--; |
| 72 | + } |
| 73 | + size--; |
| 74 | + updateMiddle(previous); |
| 75 | + } |
| 76 | + |
| 77 | + private void updateMiddle(int previousIndex){ |
| 78 | + |
| 79 | + int current = expectedIndexOfMiddle(); |
| 80 | + if(current == previousIndex){ |
| 81 | + //nothing to do |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + if(previousIndex < 0){ |
| 86 | + //from empty to 1 element |
| 87 | + assert size() == 1; |
| 88 | + middle = head; |
| 89 | + } else if(current < 0){ |
| 90 | + //from 1 element to empty |
| 91 | + assert size() == 0; |
| 92 | + middle = null; |
| 93 | + } else if(current < previousIndex){ |
| 94 | + //move backwards |
| 95 | + middle = middle.previous; |
| 96 | + } else { |
| 97 | + //move forward |
| 98 | + middle = middle.next; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private ListNode getNode(int index){ |
| 103 | + |
| 104 | + if(index < 0 || index >= size()){ |
| 105 | + throw new IndexOutOfBoundsException(); |
| 106 | + } |
| 107 | + |
| 108 | + ListNode current = null; |
| 109 | + int counter = -1; |
| 110 | + boolean forwards = false; |
| 111 | + |
| 112 | + if(index <= size() * 0.25){ |
| 113 | + //start from head |
| 114 | + current = head; |
| 115 | + counter = 0; |
| 116 | + forwards = true; |
| 117 | + |
| 118 | + } else if(index > size() * 0.25 && index < size() * 0.75){ |
| 119 | + //from middle, either backwards or forwards |
| 120 | + current = middle; |
| 121 | + counter = expectedIndexOfMiddle(); |
| 122 | + forwards = index >= size() * 0.5; |
| 123 | + |
| 124 | + } else { |
| 125 | + //start from tail |
| 126 | + current = tail; |
| 127 | + counter = size -1; |
| 128 | + forwards = false; |
| 129 | + } |
| 130 | + |
| 131 | + if(forwards){ |
| 132 | + while(counter <= index){ |
| 133 | + |
| 134 | + if(counter == index){ |
| 135 | + return current; |
| 136 | + } |
| 137 | + |
| 138 | + current = current.next; |
| 139 | + counter++; |
| 140 | + } |
| 141 | + } else { |
| 142 | + //backward |
| 143 | + while(counter >= index){ |
| 144 | + |
| 145 | + if(counter == index){ |
| 146 | + return current; |
| 147 | + } |
| 148 | + |
| 149 | + current = current.previous; |
| 150 | + counter--; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + assert false; |
| 155 | + return null; |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public T get(int index) { |
| 160 | + return getNode(index).value; |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public void add(int index, T value) { |
| 165 | + |
| 166 | + if(index < 0 || index > size){ |
| 167 | + throw new IndexOutOfBoundsException(); |
| 168 | + } |
| 169 | + |
| 170 | + ListNode node = new ListNode(); |
| 171 | + node.value = value; |
| 172 | + |
| 173 | + if(head == null){ |
| 174 | + head = node; |
| 175 | + tail = node; |
| 176 | + |
| 177 | + } else if(index == 0){ |
| 178 | + head.previous = node; |
| 179 | + node.next = head; |
| 180 | + head = node; |
| 181 | + |
| 182 | + } else if(index == size) { |
| 183 | + tail.next = node; |
| 184 | + node.previous = tail; |
| 185 | + tail = node; |
| 186 | + |
| 187 | + } else { |
| 188 | + |
| 189 | + ListNode target = getNode(index); |
| 190 | + ListNode beforeTarget = target.previous; |
| 191 | + |
| 192 | + beforeTarget.next = node; |
| 193 | + node.previous = beforeTarget; |
| 194 | + |
| 195 | + node.next = target; |
| 196 | + target.previous = node; |
| 197 | + } |
| 198 | + |
| 199 | + int previous = expectedIndexOfMiddle(); |
| 200 | + if(index <= previous){ |
| 201 | + previous++; |
| 202 | + } |
| 203 | + size++; |
| 204 | + updateMiddle(previous); |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + public int size() { |
| 209 | + return size; |
| 210 | + } |
| 211 | +} |
0 commit comments