数据结构与算法分析作业之自己实现Java双链表

自己实现的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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package com.hegongshan.collections;
import java.util.NoSuchElementException;
/**
* 双链表
* @author hegongshan https://www.hegongshan.com
* @param <E>
*/
public class DoubleLinkedList<E> {
private int size = 0;
private Node<E> first;
private Node<E> last;

private static class Node<E> {
Node<E> prev;
E data;
Node<E> next;
Node(Node<E> prev, E data, Node<E> next) {
super();
this.prev = prev;
this.data = data;
this.next = next;
}
}

public DoubleLinkedList(){

}

public int size() {
return size;
}

public boolean isEmpty() {
return first == null;
}

public boolean add(E e) {
linkLast(e);
return true;
}

public void add(int index,E e) {
checkPositionIndex(index);
if(index == size) {
linkLast(e);
} else {
linkBefore(e,node(index));
}
}

public E get(int index) {
checkElementIndex(index);
return node(index).data;
}

public E getFirst() {
if(first == null) {
throw new NoSuchElementException();
}
return first.data;
}

public E getLast() {
if(last == null) {
throw new NoSuchElementException();
}
return last.data;
}

public E set(int index,E e) {
checkElementIndex(index);
Node<E> node = node(index);
E oldValue = node.data;
node.data = e;
return oldValue;
}

public E remove(int index) {
checkElementIndex(index);
Node<E> node = node(index);
node.prev.next = node.next;
node.next.prev = node.prev;
E e = node.data;
node.prev = null;
node.next = null;
node.data = null;
size--;
return e;
}

public E removeFirst() {
if(first == null) {
throw new NoSuchElementException();
}
Node<E> node = first;
E e = node.data;
first = node.next;
node.next = null;
node.data = null;
if(first == null) {
last = null;
} else {
first.prev = null;
}
size--;
return e;
}

public E removeLast() {
if(last == null) {
throw new NoSuchElementException();
}
Node<E> node = last;
E e = node.data;
last = node.prev;
node.prev = null;
node.data = null;
if(last == null) {
first = null;
} else {
last.next = null;
}
size--;
return e;
}

public void clear() {
for(Node<E> node = first;node != null;) {
Node<E> next = node.next;
node.prev = null;
node.data = null;
node.next = null;
node = next;
}
first = last = null;
size = 0;
}

public void reverse() {
Node<E> temp = first;
first = last;
last = temp;
}

public boolean contains(Object obj) {
return indexOf(obj) != -1;
}

public int indexOf(Object obj) {
int index = 0;
if(obj == null) {
for(Node<E> node = first;node != null;node = node.next) {
if(node.data == null) {
return index;
}
index++;
}
} else {
for(Node<E> node = first;node != null;node = node.next) {
if(obj.equals(node.data)) {
return index;
}
index++;
}
}
return -1;
}

public int lastIndexOf(Object obj) {
int index = size - 1;
if(obj == null) {
for(Node<E> node = last;node != null;node = node.prev) {
if(node.data == null) {
return index;
}
index--;
}
} else {
for(Node<E> node = last;node != null;node = node.prev) {
if(obj.equals(node.data)) {
return index;
}
index--;
}
}
return -1;
}

public void linkFirst(E e) {
Node<E> node = new Node<>(null,e,first);
first.prev = node;
first = node;
size++;
}

private void linkBefore(E e,Node<E> node) {
Node<E> newNode = new Node<>(node.prev,e,node);
node.prev.next = newNode;
node.prev = newNode;
size++;
}

public void linkLast(E e) {
if(size == 0) {
first = new Node<>(null,e,null);
last = first;
size++;
return ;
}
Node<E> node = new Node<E>(last,e,null);
last.next = node;
last = node;
size++;
}

private Node<E> node(int index) {
checkElementIndex(index);
if(index < (size >> 1)) {
Node<E> node = first;
for(int i = 0 ; i < index ; i++) {
node = node.next;
}
return node;
} else {
Node<E> node = last;
for(int i = size - 1 ; i > index ; i--) {
node = node.prev;
}
return node;
}
}

private void checkElementIndex(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("index:" + index + ",size:" + size);
}
}

// 可以添加结点的位置,索引从0开始到size
private void checkPositionIndex(int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("index:" + index + ",size:" + size);
}
}
}

----------本文结束感谢您的阅读----------
坚持原创技术分享,您的支持将鼓励我继续创作!