-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.java
201 lines (157 loc) · 4.29 KB
/
LinkedList.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
public class LinkedList {
private Node head;
private Node tail;
private int size;
public LinkedList() {
this.head = null;
this.tail = null;
this.size = 0;
}
public boolean isEmpty() {
return this.head == null;
}
public int size() {
return this.size;
}
public int addLast(int node) {
Node newNode = new Node(node);
if (isEmpty()) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail.setNext(newNode);
newNode.setPrev(this.tail);
this.tail = newNode;
}
this.size += 1;
return node;
}
public int addFirst(int node) {
Node newNode = new Node(node);
if (isEmpty()) {
this.head = newNode;
this.tail = newNode;
} else {
newNode.setNext(this.head);
this.head.setPrev(newNode);
this.head = newNode;
}
this.size += 1;
return node;
}
public int add(int index, int node) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
Node newNode = new Node(node);
if (index == 0) {
return addFirst(node);
} else if (index == this.size - 1){
return addLast(node);
}
else {
Node aux = this.head;
for (int i = 0; i < index - 1; i++) {
aux = aux.getNext();}
newNode.setNext(aux.getNext());
aux.setNext(newNode);
this.size += 1;
}
return node;
}
public Node getFirst() {
if (isEmpty()) {return null;}
return this.head;
}
public Node getLast() {
if (isEmpty()) {return null;}
return this.tail;
}
public Node get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
Node aux = this.head;
for (int i = 0; i < index; i++) {
aux = aux.getNext();
}
return aux;
}
public int indexOf(int nodeValue) {
Node node = new Node(nodeValue);
Node aux = this.head;
int idx = 0;
while (aux.getNext() != null) {
if (aux.equals(node)) {
return idx;
} else {
aux = aux.getNext();
idx += 1;
}
}
return -1;
}
public boolean contains(int node) {
return indexOf(node) != -1;
}
public Node removeFirst() {
if (isEmpty()) {return null;}
Node out = this.head;
if (this.size == 1) {
this.head = null;
this.tail = null;
} else {
this.head = this.head.getNext();
this.head.setPrev(null);
}
this.size -= 1;
return out;
}
public Node removeLast() {
if (isEmpty()) {return null;}
Node out = this.tail;
if (this.size == 1) {
this.head = null;
this.tail = null;
} else {
this.tail = tail.getPrev();
this.tail.setNext(null);
}
this.size -= 1;
return out;
}
public Node removeIdx(int index) {
if (index < 0 || index >= this.size) {throw new IndexOutOfBoundsException();}
if (index == 0 ) {return removeFirst();}
if (index == this.size - 1) {return removeLast();}
Node out = this.head;
for (int i = 0; i < index; i++) {
out = out.getNext();
}
Node newNext = out.getNext();
Node newPrev = out.getPrev();
newNext.setPrev(newPrev);
newPrev.setNext(newNext);
this.size -= 1;
return out;
}
public boolean removeValue(int node) {
try {
removeIdx(indexOf(node));
} catch (Exception e) {
return false;
}
return true;
}
@Override
public String toString() {
if (isEmpty()) {return "lista vazia";}
String str = "list: ";
Node aux = this.head;
for (int i = 0; i < this.size; i++) {
str += aux.toString() + "|";
aux = aux.getNext();
}
return str;
}
}