/
MyLinkedList.java
197 lines (175 loc) · 4.35 KB
/
MyLinkedList.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
package com.eru.sourcecode.custom.linkedList;
/**
* 实简单现一个LinkedList
* Created by eru on 2020/6/17.
*/
public class MyLinkedList<E> {
/** Node size */
private int size;
/** 头节点 */
private Node<E> first;
/** 尾节点 */
private Node<E> last;
/**
* 根据传入的对象删除Node
* @param o 传入的对象
* @return 被删除的Node.item
*/
public E remove(Object o){
int index = indexOf(o);
checkRange(index);
return unlink(index);
}
/**
* 根据传入的下标删除Node
* @param index 下标
* @return 被删除的Node.item
*/
public E remove(int index){
checkRange(index);
return unlink(index);
}
/**
* 1.删除的Node为头节点
* 2.删除的Node为尾结点
* 3.删除的Node不为头节点也不为尾结点
* @param index 角标
* @return Node.item
*/
private E unlink(int index) {
Node<E> current = binarySearch(index);
final E item = current.item;
final Node<E> prevNode = current.prev;
final Node<E> nextNode = current.next;
if (prevNode == null){
first = nextNode;
}else{
prevNode.next = nextNode;
current.next = null;
}
if (nextNode == null){
last = prevNode;
}else{
nextNode.prev = prevNode;
current.prev = null;
}
size--;
return item;
}
/**
* 根据object查找对应的下标
* @param o 传入的对象
* @return 下标
*/
public int indexOf(Object o){
int index = 0;
if (o == null){
for (Node<E> f = first; f != null; f = f.next){
if (f.item == null){
return index;
}
index++;
}
}else{
for (Node<E> f = first; f != null; f = f.next){
if (o.equals(f.item)){
return index;
}
index++;
}
}
return -1;
}
/**
* 根据角标获取Node的值
* @param index 角标
* @return E
*/
public E get(int index){
checkRange(index);
return binarySearch(index).item;
}
private void checkRange(int index) {
if (index >= size || index < 0){
throw new IndexOutOfBoundsException("角标越界index=" + index + "size=" + size);
}
}
/**
* 二分查找
* @param index 角标
* @return Node
*/
Node<E> binarySearch(int index){
if (index < (size >> 1)){
Node<E> f = first;
for (int i = 0; i < index; i++) {
f = f.next;
}
return f;
}else{
Node<E> l = last;
for (int i = size - 1; i > index; i--) {
l = l.prev;
}
return l;
}
}
/**
* 添加e到最前
* @param e element
* @return boolean
*/
public boolean addFirst(E e){
LinkFirst(e);
return true;
}
void LinkFirst(E e){
final Node<E> f = first;
Node<E> newNode = new Node<>(null, f, e);
first = newNode;
if (f == null){
last = newNode;
}else{
f.prev = newNode;
}
size++;
}
/**
* 添加e到最后
* @param e element
* @return boolean
*/
public boolean addLast(E e){
linkLast(e);
return true;
}
void linkLast(E e) {
// 如果尾结点为null, 这时候头尾节点都是null, 那么新增的node就是尾结点(也是头节点)
// 不为空那么尾结点指向新node
final Node<E> l = last;
Node<E> newNode = new Node<>(l, null, e);
last = newNode;
if (l == null){
first = newNode;
}else{
l.next = newNode;
}
size++;
}
/**
* 链表中存储的Node
*/
class Node<E>{
/** 前一个node */
private Node<E> prev;
/** 后一个node */
private Node<E> next;
/** 存储的元素 */
private E item;
public Node(Node<E> prev, Node<E> next, E item) {
this.prev = prev;
this.next = next;
this.item = item;
}
}
}