-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoubleLinkedList.java
192 lines (157 loc) · 3.89 KB
/
DoubleLinkedList.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
package com.moon.classicalalgorithm.linkedlist;
/**
* @author Chanmoey
* @date 2023年04月14日
*/
public class DoubleLinkedList<E> {
public class Node {
private E value;
private Node prev;
private Node next;
public Node(E value) {
this.value = value;
}
public Node(E value, Node prev, Node next) {
this.value = value;
this.prev = prev;
this.next = next;
}
@Override
public String toString() {
return value.toString();
}
}
private final Node dummyHead;
private Node tail;
private int size;
public DoubleLinkedList() {
this.dummyHead = new Node(null);
tail = dummyHead;
this.size = 0;
}
public int getSize() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public void add(int idx, E e) {
checkAddIdx(idx);
Node pre = dummyHead;
for (int i = 0; i < idx; i++) {
pre = pre.next;
}
Node newNode = new Node(e, pre, pre.next);
if (pre == tail) {
tail = newNode;
} else {
pre.next.prev = newNode;
}
pre.next = newNode;
size++;
}
public void addFirst(E e) {
add(0, e);
}
public void addLast(E e) {
add(getSize(), e);
}
public E get(int idx) {
checkAddIdx(idx);
return getNode(idx).value;
}
public Node getNode(int idx) {
Node cur = dummyHead.next;
for (int i = 0; i < idx; i++) {
cur = cur.next;
}
return cur;
}
public E getFirst() {
return get(0);
}
public E getLast() {
return get(getSize() - 1);
}
public void set(int idx, E e) {
checkAddIdx(idx);
Node node = getNode(idx);
node.value = e;
}
public Node getNode(E e) {
Node cur = dummyHead.next;
while (cur != null) {
if (cur.value.equals(e)) {
return cur;
}
cur = cur.next;
}
return null;
}
public boolean contains(E e) {
return getNode(e) != null;
}
public void remove(int idx) {
checkRemoveIdx(idx);
Node cur = dummyHead.next;
for (int i = 0; i < idx; i++) {
cur = cur.next;
}
remove(cur);
}
public void removeFirst() {
remove(0);
}
public void removeLast() {
remove(getSize() - 1);
}
public void removeElement(E e) {
Node cur = dummyHead.next;
while (cur != null) {
if (cur.value.equals(e)) {
break;
}
cur = cur.next;
}
if (cur == null) {
return;
}
remove(cur);
}
public void remove(Node node) {
size--;
if (node == tail) {
tail = node.prev;
// 断开连接 Help GC
node.prev = null;
tail.next = null;
return;
}
node.prev.next = node.next;
node.next.prev = node.prev;
// Help GC
node.prev = null;
node.next = null;
}
private void checkRemoveIdx(int idx) {
if (idx < 0 || idx >= size) {
throw new IndexOutOfBoundsException(idx + " is out of bound in [0, " + (size - 1) + "]");
}
}
private void checkAddIdx(int idx) {
if (idx < 0 || idx > size) {
throw new IndexOutOfBoundsException(idx + " is out of bound in [0, " + size + "]");
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Node cur = dummyHead.next;
while (cur != null) {
sb.append(cur).append(" <-> ");
cur = cur.next;
}
sb.append("null");
return sb.toString();
}
}