|
1 | 1 | package com.guodong.datastructure;
|
2 | 2 |
|
3 | 3 | public class LinkedList implements List {
|
4 |
| - |
| 4 | + |
| 5 | + private int size; |
| 6 | + |
5 | 7 | private Node head;
|
6 |
| - |
7 |
| - public void add(Object o){ |
8 |
| - |
| 8 | + |
| 9 | + private Node last; |
| 10 | + |
| 11 | + /** |
| 12 | + * 向 链表尾端插入元素 |
| 13 | + * |
| 14 | + * @Method add |
| 15 | + * @param o |
| 16 | + * @see com.guodong.datastructure.List#add(java.lang.Object) |
| 17 | + */ |
| 18 | + public void add(Object o) { |
| 19 | + linkLast(o); |
9 | 20 | }
|
10 |
| - public void add(int index , Object o){ |
11 |
| - |
| 21 | + |
| 22 | + /** |
| 23 | + * 向链表指定位置插入元素 |
| 24 | + * |
| 25 | + * @Method add |
| 26 | + * @param index |
| 27 | + * @param o |
| 28 | + * @see com.guodong.datastructure.List#add(int, java.lang.Object) |
| 29 | + */ |
| 30 | + public void add(int index, Object o) { |
| 31 | + checkIndexForAdd(index); |
| 32 | + |
| 33 | + if (index == size) { |
| 34 | + linkLast(o); |
| 35 | + } else { |
| 36 | + Node prevNode = getNodeByIndex(index - 1); // 取到当前下标的前一个节点 |
| 37 | + Node currentNode = getNodeByIndex(index); // 取到当前下标节点 |
| 38 | + Node newNode = new Node(o, currentNode); // 创建新节点,新节点的下一个节点为当前下标节点 |
| 39 | + |
| 40 | + if (prevNode == null) { // 如果前一个节点为空,说明从头部插入 |
| 41 | + head = newNode; |
| 42 | + } else { |
| 43 | + prevNode.next = newNode; |
| 44 | + } |
| 45 | + size++; |
| 46 | + } |
12 | 47 | }
|
13 |
| - public Object get(int index){ |
14 |
| - return null; |
| 48 | + |
| 49 | + /** |
| 50 | + * 根据下标获取链表中元素 |
| 51 | + * |
| 52 | + * @Method get |
| 53 | + * @param index |
| 54 | + * @return |
| 55 | + * @see com.guodong.datastructure.List#get(int) |
| 56 | + */ |
| 57 | + public Object get(int index) { |
| 58 | + checkIndexForGet(index); |
| 59 | + return getNodeByIndex(index).data; |
15 | 60 | }
|
16 |
| - public Object remove(int index){ |
| 61 | + |
| 62 | + public Object remove(int index) { |
17 | 63 | return null;
|
18 | 64 | }
|
19 |
| - |
20 |
| - public int size(){ |
21 |
| - return -1; |
| 65 | + |
| 66 | + public int size() { |
| 67 | + return size; |
22 | 68 | }
|
23 |
| - |
24 |
| - public void addFirst(Object o){ |
25 |
| - |
| 69 | + |
| 70 | + public void addFirst(Object o) { |
| 71 | + |
26 | 72 | }
|
27 |
| - public void addLast(Object o){ |
28 |
| - |
| 73 | + |
| 74 | + public void addLast(Object o) { |
| 75 | + linkLast(o); |
29 | 76 | }
|
30 |
| - public Object removeFirst(){ |
| 77 | + |
| 78 | + public Object removeFirst() { |
31 | 79 | return null;
|
32 | 80 | }
|
33 |
| - public Object removeLast(){ |
| 81 | + |
| 82 | + public Object removeLast() { |
34 | 83 | return null;
|
35 | 84 | }
|
36 |
| - public Iterator iterator(){ |
| 85 | + |
| 86 | + public Iterator iterator() { |
37 | 87 | return null;
|
38 | 88 | }
|
| 89 | + |
| 90 | + /** |
| 91 | + * 根据下标获取对应的节点 |
| 92 | + * |
| 93 | + * @MethodName getNodeByIndex |
| 94 | + * @author zhaogd |
| 95 | + * @date 2017年2月23日 下午3:32:48 |
| 96 | + * @param index |
| 97 | + * @return |
| 98 | + */ |
| 99 | + private Node getNodeByIndex(int index) { |
| 100 | + Node n = head; |
| 101 | + for (int i = 0; i < index; i++) { |
| 102 | + n = n.next; |
| 103 | + } |
| 104 | + return n; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * 在链表尾端插入节点 |
| 109 | + * |
| 110 | + * @MethodName linkLast |
| 111 | + * @author zhaogd |
| 112 | + * @date 2017年2月23日 下午3:14:28 |
| 113 | + * @param o |
| 114 | + */ |
| 115 | + private void linkLast(Object o) { |
| 116 | + Node n = last; // 取出原尾端数据 |
| 117 | + Node newNode = new Node(o, null); // 创建新节点 |
| 118 | + last = newNode; // 把新节点放入链表尾端 |
| 119 | + // 如果原尾端为空,说明链表为空,把新节点也放入链表头部 |
| 120 | + // 如果不为空,把原尾端节点指向新节点 |
| 121 | + if (n == null) { |
| 122 | + head = newNode; |
| 123 | + } else { |
| 124 | + n.next = newNode; |
| 125 | + } |
| 126 | + |
| 127 | + size++; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * 检查下标是否合法 |
| 132 | + * |
| 133 | + * @MethodName checkIndexForAdd |
| 134 | + * @author zhaogd |
| 135 | + * @date 2017年2月23日 下午3:05:07 |
| 136 | + * @param index |
| 137 | + */ |
| 138 | + private void checkIndexForAdd(int index) { |
| 139 | + if (index < 0 || index > size) { |
| 140 | + throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); |
| 141 | + } |
| 142 | + } |
39 | 143 |
|
40 |
| - |
41 |
| - private static class Node{ |
| 144 | + /** |
| 145 | + * 检查下标是否合法 |
| 146 | + * |
| 147 | + * @MethodName checkIndexForGet |
| 148 | + * @author zhaogd |
| 149 | + * @date 2017年2月23日 下午4:21:35 |
| 150 | + * @param index |
| 151 | + */ |
| 152 | + private void checkIndexForGet(int index) { |
| 153 | + if (index < 0 || index >= size) { |
| 154 | + throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private static class Node { |
42 | 159 | Object data;
|
43 | 160 | Node next;
|
| 161 | + |
| 162 | + Node(Object data, Node next) { |
| 163 | + this.data = data; |
| 164 | + this.next = next; |
| 165 | + } |
44 | 166 | }
|
45 | 167 | }
|
0 commit comments