数据结构与算法分析作业之自己实现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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package com.hegongshan.collections;
import java.util.NoSuchElementException;
/**
* 单链表
* @author hegongshan https://www.hegongshan.com
* @param <E>
*/
public class SingleLinkedList<E> {

private int size = 0;
private Node<E> first;

public SingleLinkedList() {
}

// 单链表中元素个数
public int size() {
return size;
}

// 判断单链表是否为空
public boolean isEmpty() {
return first == null;
}

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

public Object[] toArray() {
if (isEmpty()) {
return null;
}
Object[] array = new Object[size];
Node<E> node = first;
for (int i = 0; i < size; i++) {
array[i] = node.data;
node = node.next;
}
return array;
}

@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
return (T[]) toArray();
}

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

// 在单链表尾部插入新的结点
public void linkLast(E e) {
if (!isEmpty()) {
Node<E> newNode = new Node<>(e, null);
Node<E> last = first;
while (last.next != null) {
last = last.next;
}
last.next = newNode;
} else {
first = new Node<>(e, null);
}
size++;
}

// 在单链表头部插入新的数据
public void linkFirst(E e) {
Node<E> newNode = new Node<>(e, first);
first = newNode;
size++;
}

// 清空单链表
public void clear() {
for (Node<E> node = first; node != null;) {
Node<E> next = node.next;
node.data = null;
node.next = null;
node = next;
}
first = null;
size = 0;
}

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

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

// 把索引号为index的结点的数据改为element,并返回原来的结点数据
public E set(int index, E element) {
checkElementIndex(index);
Node<E> node = node(index);
E oldElement = node.data;
node.data = element;
return oldElement;
}

// 在指定索引处插入元素
public void add(int index, E element) {
checkPositionIndex(index);
if (index == 0) {
linkFirst(element);
return;
}
Node<E> newNode = new Node<>(element, null);
Node<E> node = first;
for (int i = 0; i < index - 1; i++) {
node = node.next;
}
newNode.next = node.next;
node.next = newNode;
size++;
}

// 删除指定索引的结点,并返回该结点的数据值
public E remove(int index) {
checkElementIndex(index);
Node<E> node = first;
for (int i = 0; i < index - 1; i++) {
node = node.next;
}
Node<E> removeNode = node.next;
node.next = removeNode.next;
E e = removeNode.data;
removeNode.data = null;
removeNode.next = null;
return e;
}

//反转单链表pre->p->next
public void reverse() {
Node<E> pre = first;
Node<E> p = first.next;
Node<E> next = null;
pre.next = null;
while(p != null) {
next = p.next;
p.next = pre;
pre = p;
p = next;
}
first = pre;
}

// 给定数据在单链表中首次出现的位置
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 = 0;
int i = 0;
int[] array = new int[size];
if (obj == null) {
for (Node<E> node = first; node != null; node = node.next) {
if (node.data == null) {
array[i++] = index;
}
index++;
}
} else {
for (Node<E> node = first; node != null; node = node.next) {
if (obj.equals(node.data)) {
array[i++] = index;
}
index++;
}
}
if (array.length != 0) {
return array[i - 1];
}
return -1;
}

/*public int lastIndexOf(Object obj) {
Object[] array = toArray();
if(obj == null) {
for(int i = array.length -1;i>=0;i--) {
if(array[i] == null) {
return i;
}
}
} else {
for(int i = array.length -1;i>=0;i--) {
if(obj.equals(array[i])) {
return i;
}
}
}
return -1;
}*/

@Override
public String toString() {
StringBuilder list = new StringBuilder("[");
Node<E> node = first;
int index = 0;
while (node != null) {
index++;
if (index == size) {
list.append(node.data);
} else {
list.append(node.data).append(",");
}
node = node.next;
}
list.append("]");
return list.toString();
}

private Node<E> node(int index) {
checkElementIndex(index);
Node<E> node = first;
for(int i = 0 ; i < index ;i++) {
node = node.next;
}
return node;
}

private static class Node<E> {
private E data;
private Node<E> next;

Node(E data, Node<E> next) {
this.data = data;
this.next = next;
}
}

// 单链表中的结点索引从0开始到size-1
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);
}
}
}

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