-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList2.java
More file actions
294 lines (252 loc) · 7.31 KB
/
LinkedList2.java
File metadata and controls
294 lines (252 loc) · 7.31 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import java.util.*;
public class LinkedList2
{
public Node head;
public Node tail;
public LinkedList2()
{
head = null;
tail = null;
}
public void addInTail(Node _item)
{
if (head == null) {
this.head = _item;
this.head.next = null;
this.head.prev = null;
} else {
this.tail.next = _item;
_item.prev = tail;
}
this.tail = _item;
}
public Node find(int _value) {
Node node = this.getHead();
while (node != null) {
if (node.getValue() == _value)
return node;
node = node.getNext();
}
return null;
}
public ArrayList<Node> findAll(int _value) {
ArrayList<Node> nodes = new ArrayList<Node>();
Node node = this.getHead();
while (node != null) {
if (node.getValue() == _value) {
nodes.add(node);
}
node = node.getNext();
}
return nodes;
}
public boolean remove(int _value) {
if (this.getHead() == null) {
return false;
}
if (this.getHead().getValue() == _value) {
this.setHead(this.getHead().getNext());
if (this.getHead() == null) {
this.setTail(null);
} else {
this.getHead().setPrev(null);
}
return true;
}
Node index = this.getHead();
while (index != null) {
if (index.getValue() == _value) {
index.getPrev().setNext(index.getNext());
if (index.getNext() == null) {
this.setTail(index.getPrev());
} else {
index.getNext().setPrev(index.getPrev());
}
return true;
}
index = index.getNext();
}
return false;
}
public void removeAll(int _value) {
if (this.getHead() == null) {
return;
}
while (this.getHead().getValue() == _value) {
this.setHead(this.getHead().getNext());
if (this.getHead() == null) {
this.setTail(null);
return;
} else {
this.getHead().setPrev(null);
}
}
Node index = this.getHead();
while (index != null) {
if (index.getValue() == _value) {
index.getPrev().setNext(index.getNext());
if (index.getNext() == null) {
this.setTail(index.getPrev());
} else {
index.getNext().setPrev(index.getPrev());
}
}
index = index.getNext();
}
}
public void clear() {
this.setHead(null);
this.setTail(null);
}
public int count() {
int i = 0;
Node node = this.getHead();
while (node != null) {
i++;
node = node.getNext();
}
return i;
}
public void insertAfter(Node _nodeAfter, Node _nodeToInsert) {
if (this.getHead() == null && _nodeAfter == null) {
this.setHead(_nodeToInsert);
this.setTail(_nodeToInsert);
return;
}
if (this.getHead() == null && _nodeAfter != null) {
return;
}
if (_nodeAfter == null) {
_nodeToInsert.setNext(this.getHead());
this.getHead().setPrev(_nodeToInsert);
this.setHead(_nodeToInsert);
return;
}
Node index = this.getHead();
while (index != null) {
if (_nodeAfter.equals(index)) {
_nodeToInsert.setNext(_nodeAfter.getNext());
_nodeToInsert.setPrev(_nodeAfter);
_nodeAfter.setNext(_nodeToInsert);
if (_nodeToInsert.next == null) {
this.setTail(_nodeToInsert);
} else {
_nodeToInsert.getNext().setPrev(_nodeToInsert);
}
return;
}
index = index.next;
}
}
public Node getHead() {
return head;
}
public void setHead(Node head) {
this.head = head;
}
public Node getTail() {
return tail;
}
public void setTail(Node tail) {
this.tail = tail;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LinkedList2 that = (LinkedList2) o;
Node nodeThis = this.getHead();
Node nodeThat = that.getHead();
if (nodeThis == null && nodeThat == null) {
return true;
}
if (nodeThis == null ^ nodeThat == null) {
return false;
}
if (nodeThis.getValue() != nodeThat.getValue()
|| this.getTail().getValue() != that.getTail().getValue()) {
return false;
}
while (nodeThat != null && nodeThis != null) {
if (nodeThat.getValue() != nodeThis.getValue()) {
return false;
}
nodeThat = nodeThat.getNext();
nodeThis = nodeThis.getNext();
}
if (nodeThat == null ^ nodeThis == null) {
return false;
}
nodeThat = that.getTail();
nodeThis = this.getTail();
while (nodeThat != null && nodeThis != null) {
if (nodeThat.getValue() != nodeThis.getValue()) {
return false;
}
nodeThat = nodeThat.getPrev();
nodeThis = nodeThis.getPrev();
}
if (nodeThat == null ^ nodeThis == null) {
return false;
}
return true;
}
}
class Node
{
public int value;
public Node next;
public Node prev;
public Node(int _value)
{
value = _value;
next = null;
prev = null;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Node node = (Node) o;
if (this.getValue() != node.getValue()) return false;
if (this.getNext() == null ^ node.getNext() == null
|| this.getPrev() == null ^ node.getPrev() == null) {
return false;
}
if (this.getNext() == null && node.getNext() == null) {
if (this.getPrev() == null && node.getPrev() == null) {
return true;
} else {
if (this.getPrev().getValue() == node.getPrev().getValue()) {
return true;
} else {
return false;
}
}
}
if (this.getPrev() == null && node.getPrev() == null) {
if (this.getNext().getValue() == node.getNext().getValue()) {
return true;
}
}
return false;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public Node getPrev() {
return prev;
}
public void setPrev(Node prev) {
this.prev = prev;
}
}