链表是一种在计算机科学中十分重要的数据结构,他是很多高级数据结构的实现基础,可以这样说,没有链表,就会少很多在计算机科学领域大放异彩的优秀数据结构。

本文提供了一种由 Java 语言实现链表的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
public class DoubleLinkedList {

private class Node {
private Object value;
private Node prev;
private Node next;

public Node(Object value) {
this.value = value;
}

@Override
public String toString() {
return this.value.toString();
}

}

private int size = 0;

private Node head = null;

/**
* Get length of this list.
*
* @return length
*/
public int length() {
return this.size;
}

@Override
public String toString() {
if (size == 0) {
return null;
} else {
StringBuffer result = new StringBuffer();
result.append("[");
Node node = head;
for (int i = 0; i < size; i++) {
result.append(node.value + ", ");
node = node.next;
}
String s = result.substring(0, result.length() - 2) + "]";
return s;
}
}

/**
* <b>Insert</b> value into exact position.
*
* @param value Add value.
* @param index The index of the position.
*/
public void insert(Object value, int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("下标越界");
} else {
Node newNode = new Node(value);
if (size == 0) {
head = newNode;
} else if (size == index) {
Node node = head;
for (int i = 0; i < index - 1; i++) {
node = node.next;
}
node.next = newNode;
newNode.prev = node;
} else {
Node node = head;
for (int i = 0; i < index; i++) {
node = node.next;
}
// 新插入进来的节点
newNode.next = node;
node.prev.next = newNode;
newNode.prev = node.prev;
node.prev = newNode;
}
size++;
}
}

/**
* <b>Add</b> value at the last of this list.
*
* @param value Add value.
*/
public void add(Object value) {
this.insert(value, size);
}

/**
* Delete the first value of been inputted.
*
* @param value
* @return
*/
public Object remove(Object value) {
if (size == 0) {
throw new IndexOutOfBoundsException("没有该参数");
} else {
if (size == 1) {
head = null;
return head.value;
} else {
Node node = head;
boolean flag = false;
for (int i = 0; i < size; i++) {
if (value.equals(node.value)) {
if (i == 0) {
node.next.prev = null;
head = node.next;
} else if (i == size - 1) {
node.prev.next = null;
} else {
node.prev.next = node.next;
node.next.prev = node.prev;
}
flag = true;
size--;
break;
} else {
node = node.next;
}
}
if (!flag) {
throw new IndexOutOfBoundsException("没有该参数");
} else {
return value;
}
}
}
}

/**
* Delete the value
*
* @param index
* @return
*/
public Object remove(int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("下标越界");
} else {
Object value = null;
if (size == 0) {
// throw new NoSuchFieldException("链表长度为空");
} else if (size == 1) {
value = head.value;
head = null;
size--;
} else {
if (index == 0) {
value = head.value;
head.next.prev = null;
head = head.next;
} else {
Node node = head;
for (int i = 0; i < index; i++) {
node = node.next;
}
if (index == size - 1) {
node.prev.next = null;
value = node.value;
} else {
node.prev.next = node.next;
node.next.prev = node.prev;
value = node.value;
}
}
size--;
}
return value;
}
}

/**
* Check this list is empty.
*
* @return
*/
public boolean isEmpty() {
return size == 0 ? true : false;
}

/**
* Set this list empty
*
* @return
*/
public DoubleLinkedList empty() {
head = null;
size = 0;
return this;
}

public static void main(String[] args) {
DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
doubleLinkedList.add("100");
doubleLinkedList.insert("200", 1);
doubleLinkedList.add("1221");
doubleLinkedList.remove(0);
System.out.println(doubleLinkedList);
}

}