Skip to content

Commit 9f2a6e3

Browse files
committed
Merge remote-tracking branch 'refs/remotes/guodongym/master'
2 parents 77348a7 + 9f1df17 commit 9f2a6e3

File tree

1 file changed

+144
-22
lines changed

1 file changed

+144
-22
lines changed
Lines changed: 144 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,167 @@
11
package com.guodong.datastructure;
22

33
public class LinkedList implements List {
4-
4+
5+
private int size;
6+
57
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);
920
}
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+
}
1247
}
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;
1560
}
16-
public Object remove(int index){
61+
62+
public Object remove(int index) {
1763
return null;
1864
}
19-
20-
public int size(){
21-
return -1;
65+
66+
public int size() {
67+
return size;
2268
}
23-
24-
public void addFirst(Object o){
25-
69+
70+
public void addFirst(Object o) {
71+
2672
}
27-
public void addLast(Object o){
28-
73+
74+
public void addLast(Object o) {
75+
linkLast(o);
2976
}
30-
public Object removeFirst(){
77+
78+
public Object removeFirst() {
3179
return null;
3280
}
33-
public Object removeLast(){
81+
82+
public Object removeLast() {
3483
return null;
3584
}
36-
public Iterator iterator(){
85+
86+
public Iterator iterator() {
3787
return null;
3888
}
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+
}
39143

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 {
42159
Object data;
43160
Node next;
161+
162+
Node(Object data, Node next) {
163+
this.data = data;
164+
this.next = next;
165+
}
44166
}
45167
}

0 commit comments

Comments
 (0)