|
| 1 | +package firstHomeWork.util; |
| 2 | + |
| 3 | +import java.util.NoSuchElementException; |
| 4 | + |
| 5 | +/** |
| 6 | + * @Description: 双向链表 |
| 7 | + * @author: leijing |
| 8 | + * @date: 2017年2月24日 上午11:37:58 |
| 9 | + * @param <E> |
| 10 | + */ |
| 11 | +public class DoubleLinkedList<E> { |
| 12 | + |
| 13 | + private int size;//节点个数 |
| 14 | + private Node<E> head;//头节点 |
| 15 | + private Node<E> tail;//尾节点 |
| 16 | + |
| 17 | + public Node<E> getHead() { |
| 18 | + return head; |
| 19 | + } |
| 20 | + public void setHead(Node<E> head) { |
| 21 | + this.head = head; |
| 22 | + } |
| 23 | + public Node<E> getTail() { |
| 24 | + return tail; |
| 25 | + } |
| 26 | + public void setTail(Node<E> tail) { |
| 27 | + this.tail = tail; |
| 28 | + } |
| 29 | + /** |
| 30 | + * @Description: 添加元素到头部 |
| 31 | + * @param e |
| 32 | + * @return: boolean |
| 33 | + * @author: leijing |
| 34 | + * @date: 2017年2月24日 上午11:38:20 |
| 35 | + */ |
| 36 | + public boolean addFirst(E e) { |
| 37 | + Node<E> node = new Node<E>(e); |
| 38 | + if(null == head){ |
| 39 | + node.prev = null; |
| 40 | + head = node; |
| 41 | + tail = head; |
| 42 | + }else{ |
| 43 | + node.next = head; |
| 44 | + head.prev = node; |
| 45 | + head = node; |
| 46 | + } |
| 47 | + size++; |
| 48 | + return true; |
| 49 | + } |
| 50 | + /** |
| 51 | + * @Description: 添加元素到尾部 |
| 52 | + * @param e |
| 53 | + * @return: boolean |
| 54 | + * @author: leijing |
| 55 | + * @date: 2017年2月24日 上午11:38:20 |
| 56 | + */ |
| 57 | + public boolean addLast(E e) { |
| 58 | + Node<E> node = new Node<E>(e); |
| 59 | + if(null == tail){ |
| 60 | + tail.next = null; |
| 61 | + tail = node; |
| 62 | + head = tail; |
| 63 | + }else{ |
| 64 | + tail.next = node; |
| 65 | + node.prev = tail; |
| 66 | + tail = node; |
| 67 | + } |
| 68 | + size++; |
| 69 | + return true; |
| 70 | + } |
| 71 | + |
| 72 | + public boolean remove(E o) throws Exception { |
| 73 | + if(isEmpty()){ |
| 74 | + throw new Exception("链表为空,没有元素可以删除"); |
| 75 | + } |
| 76 | + Node<E> current = head;//从头节点开始删 |
| 77 | + if(o == null){ |
| 78 | + while(current != null){ |
| 79 | + if(current.data == null){ |
| 80 | + current.prev.next = current.next;//将当前节点的前驱节点的后继节点改为当前节点的后继 |
| 81 | + current.next.prev = current.prev;//将当前节点后继节点的前驱节点改为当前节点的前驱节点 |
| 82 | + current.next = null;//当前节点的前驱改为null |
| 83 | + current.prev = null;//当前节点的后继改为null |
| 84 | + size--; |
| 85 | + return true; |
| 86 | + } |
| 87 | + current = current.next; |
| 88 | + } |
| 89 | + }else{ |
| 90 | + while(current != null){ |
| 91 | + if(o.equals(current.data)){ |
| 92 | + current.prev.next = current.next;//将当前节点的前驱节点的后继节点改为当前节点的后继 |
| 93 | + current.next.prev = current.prev;//将当前节点后继节点的前驱节点改为当前节点的前驱节点 |
| 94 | + current.next = null;//当前节点的前驱改为null |
| 95 | + current.prev = null;//当前节点的后继改为null |
| 96 | + size--; |
| 97 | + return true; |
| 98 | + } |
| 99 | + current = current.next; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @Description: 返回元素个数 |
| 108 | + * @return: int |
| 109 | + * @author: leijing |
| 110 | + * @date: 2017年2月24日 上午11:38:20 |
| 111 | + */ |
| 112 | + public int size() { |
| 113 | + return size; |
| 114 | + } |
| 115 | + /** |
| 116 | + * @Description: 是否空链表 |
| 117 | + * @return: boolean |
| 118 | + * @author: leijing |
| 119 | + * @date: 2017年2月24日 上午11:38:20 |
| 120 | + */ |
| 121 | + public boolean isEmpty() { |
| 122 | + return size == 0; |
| 123 | + } |
| 124 | + /** |
| 125 | + * @Description: 检查下标有效性 |
| 126 | + * @param index |
| 127 | + * @return: void |
| 128 | + * @author: leijing |
| 129 | + * @date: 2017年2月24日 上午11:40:15 |
| 130 | + */ |
| 131 | + private void rangeCheck(int index){ |
| 132 | + if(index < 0 || index > size){ |
| 133 | + throw new IndexOutOfBoundsException(); |
| 134 | + } |
| 135 | + } |
| 136 | + public E get(int index) { |
| 137 | + rangeCheck(index); |
| 138 | + Node<E> current = head;//从头节点开始 |
| 139 | + int i = 0; |
| 140 | + while(current != null){ |
| 141 | + if(i == index){ |
| 142 | + return current.data; |
| 143 | + } |
| 144 | + current = current.next; |
| 145 | + i++; |
| 146 | + } |
| 147 | + return null; |
| 148 | + } |
| 149 | + public Node<E> node(int index) { |
| 150 | + rangeCheck(index); |
| 151 | + if(index < size >> 1){//小于元素大小的二分之一,从头节点开始遍历 |
| 152 | + Node<E> current = head;//从头节点开始 |
| 153 | + for(int i = 0 ; i < index ; i++){ |
| 154 | + current = current.next; |
| 155 | + } |
| 156 | + return (Node<E>)current; |
| 157 | + }else{//从尾节点开始遍历 |
| 158 | + Node<E> current = tail;//从尾节点开始 |
| 159 | + for(int i = 0 ; i < index ; i++){ |
| 160 | + current = current.prev; |
| 161 | + } |
| 162 | + return (Node<E>)current; |
| 163 | + } |
| 164 | + } |
| 165 | + /** |
| 166 | + * @Description: 设置某个位置的元素 |
| 167 | + * @param index |
| 168 | + * @param data |
| 169 | + * @return: E |
| 170 | + * @author: leijing |
| 171 | + * @date: 2017年2月24日 上午11:58:32 |
| 172 | + */ |
| 173 | + public E set(int index, E data) { |
| 174 | + rangeCheck(index); |
| 175 | + Node<E> current = head;//从头节点开始 |
| 176 | + int i = 0; |
| 177 | + while(current != null){ |
| 178 | + if(i == index){ |
| 179 | + Node<E> node = new Node<E>(data); |
| 180 | + Node<E> prev = current.prev; |
| 181 | + prev.next = node; |
| 182 | + node.prev = prev; |
| 183 | + node.next = current; |
| 184 | + current.prev = node; |
| 185 | + size++; |
| 186 | + return data; |
| 187 | + } |
| 188 | + current = current.next; |
| 189 | + i++; |
| 190 | + } |
| 191 | + return null; |
| 192 | + } |
| 193 | + /** |
| 194 | + * @Description: 判断是否包含某个元素 |
| 195 | + * @param o |
| 196 | + * @throws Exception |
| 197 | + * @return: boolean |
| 198 | + * @author: leijing |
| 199 | + * @date: 2017年2月24日 上午11:57:35 |
| 200 | + */ |
| 201 | + public boolean contains(Object o) throws Exception { |
| 202 | + if(isEmpty()){ |
| 203 | + throw new Exception("链表为空,没有任何元素"); |
| 204 | + } |
| 205 | + Node<E> current = head;//从头节点开始找 |
| 206 | + if(o == null){ |
| 207 | + while(current != null){ |
| 208 | + if(current.data == null){ |
| 209 | + return true; |
| 210 | + } |
| 211 | + current = current.next; |
| 212 | + } |
| 213 | + }else{ |
| 214 | + while(current != null){ |
| 215 | + if(o.equals(current.data)){ |
| 216 | + return true; |
| 217 | + } |
| 218 | + current = current.next; |
| 219 | + } |
| 220 | + } |
| 221 | + return false; |
| 222 | + } |
| 223 | + /** |
| 224 | + * @Description: 清空链表,删除所有元素 |
| 225 | + * @return: void |
| 226 | + * @author: leijing |
| 227 | + * @date: 2017年2月24日 下午4:41:56 |
| 228 | + */ |
| 229 | + public void clear() { |
| 230 | + Node<E> current = head;//从头节点开始遍历 |
| 231 | + while(current != null){ |
| 232 | + Node<E> tmp = current; |
| 233 | + current = current.next; |
| 234 | + tmp.prev = null; |
| 235 | + tmp.next = null; |
| 236 | + } |
| 237 | + size = 0; |
| 238 | + } |
| 239 | + |
| 240 | + public Iterator<E> iterator() { |
| 241 | + return new ListItr(0); |
| 242 | + } |
| 243 | + private class ListItr implements Iterator<E>{ |
| 244 | + private Node<E> lastReturned = null;//当前的节点 |
| 245 | + private Node<E> next;//下一个节点 |
| 246 | + private int nextIndex;//当前索引的下标 |
| 247 | + |
| 248 | + public ListItr(int nextIndex){ |
| 249 | + next = (nextIndex == size) ? null : node(nextIndex); |
| 250 | + } |
| 251 | + |
| 252 | + @Override |
| 253 | + public boolean hasNext() { |
| 254 | + return nextIndex < size; |
| 255 | + } |
| 256 | + |
| 257 | + @Override |
| 258 | + public E next() { |
| 259 | + if (!hasNext()){ |
| 260 | + throw new NoSuchElementException(); |
| 261 | + } |
| 262 | + |
| 263 | + lastReturned = next; |
| 264 | + next = next.next; |
| 265 | + nextIndex++; |
| 266 | + return lastReturned.data; |
| 267 | + } |
| 268 | + |
| 269 | + @Override |
| 270 | + public void remove() { |
| 271 | + if (lastReturned == null){ |
| 272 | + throw new IllegalStateException(); |
| 273 | + } |
| 274 | + |
| 275 | + if(lastReturned == next){//tail node |
| 276 | + lastReturned.prev.next = null; |
| 277 | + lastReturned.prev = null; |
| 278 | + }else{ |
| 279 | + lastReturned.prev.next = lastReturned.next; |
| 280 | + lastReturned.next.prev = lastReturned.prev; |
| 281 | + lastReturned.next = null; |
| 282 | + lastReturned.prev = null; |
| 283 | + } |
| 284 | + nextIndex--; |
| 285 | + } |
| 286 | + |
| 287 | + public boolean hasPrev(){ |
| 288 | + return nextIndex > 0; |
| 289 | + } |
| 290 | + |
| 291 | + public E prev(){ |
| 292 | + if(!hasPrev()){ |
| 293 | + throw new NoSuchElementException(); |
| 294 | + } |
| 295 | + next = lastReturned = (next == null ) ? tail : next.prev;//如果是头节点,前一个指向尾节点 |
| 296 | + nextIndex--; |
| 297 | + return lastReturned.data; |
| 298 | + } |
| 299 | + |
| 300 | + } |
| 301 | + |
| 302 | + static class Node<E>{ |
| 303 | + private E data; |
| 304 | + private Node<E> prev;//前驱节点 |
| 305 | + private Node<E> next;//后继节点 |
| 306 | + |
| 307 | + public Node(E data){ |
| 308 | + this.data = data; |
| 309 | + } |
| 310 | + |
| 311 | + public E getData() { |
| 312 | + return data; |
| 313 | + } |
| 314 | + |
| 315 | + public void setData(E data) { |
| 316 | + this.data = data; |
| 317 | + } |
| 318 | + |
| 319 | + public Node<E> getPrev() { |
| 320 | + return prev; |
| 321 | + } |
| 322 | + |
| 323 | + public void setPrev(Node<E> prev) { |
| 324 | + this.prev = prev; |
| 325 | + } |
| 326 | + |
| 327 | + public Node<E> getNext() { |
| 328 | + return next; |
| 329 | + } |
| 330 | + |
| 331 | + public void setNext(Node<E> next) { |
| 332 | + this.next = next; |
| 333 | + } |
| 334 | + } |
| 335 | +} |
0 commit comments