forked from AllAlgorithms/java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoubleLinkedListOfInteger.java
185 lines (166 loc) · 4.35 KB
/
DoubleLinkedListOfInteger.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
public class DoubleLinkedListOfInteger {
private Node header;
private Node trailer;
private int count;
private class Node {
public Integer element;
public Node next;
public Node prev;
public Node(Integer e) {
element = e;
next = null;
prev = null;
}
}
public DoubleLinkedListOfInteger() {
header = new Node(null);
trailer = new Node(null);
header.next = trailer;
trailer.prev = header;
count = 0;
}
/**
* Add one element to the end of the list
*/
public void add(Integer element) {
Node n = new Node(element);
Node last = trailer.prev;
n.prev = last;
n.next = trailer;
last.next = n;
trailer.prev = n;
count++;
}
/**
* Remove the first ocurence of a element at the list
*/
public boolean remove(Integer element) {
boolean achou = false;
Node aux = header.next;
while (achou != true && aux != null) {
if (element.equals(aux.element)) {
aux.prev.next = aux.next;
aux.next.prev = aux.prev;
count--;
achou = true;
} else {
aux = aux.next;
}
}
return achou;
}
/**
* Remove a element at index x
*/
public Integer remove(int index) {
if ((index < 0) || (index >= count)) {
throw new IndexOutOfBoundsException("Index = " + index);
}
Node aux = null;
if (index <= count / 2) {
aux = header.next;
for (int i = 0; i < index; i++) {
aux = aux.next;
}
} else {
aux = trailer.prev;
for (int i = count - 1; i > index; i--) {
aux = aux.prev;
}
}
Integer item = aux.element;
aux.prev.next = aux.next;
aux.next.prev = aux.prev;
count--;
return item;
}
/**
* See's if the list have some element x
*/
public boolean contains(Integer element) {
Node aux = header.next;
while (aux != trailer) {
if (aux.element.equals(element)) {
return (true);
}
aux = aux.next;
}
return false;
}
/**
* Return element at index x
*/
public Integer get(int index) {
if ((index < 0) || (index >= count)) {
throw new IndexOutOfBoundsException();
}
Node aux = header.next;
int c = 0;
while (c < index) {
aux = aux.next;
c++;
}
return aux.element;
}
/**
* Return index of the ocurence of the number x
*/
public int indexOf(Integer element) {
Node aux = header.next;
for (int i = 0; i < count; i++) {
if (aux.element.equals(element)) {
return (i);
}
aux = aux.next;
}
return -1;
}
/**
* Changes the value of some index to x
*/
public Integer set(int index, Integer element) {
if ((index < 0) || (index >= count)) {
throw new IndexOutOfBoundsException();
}
Node aux = header.next;
for (int i = 0; i < index; i++) {
aux = aux.next;
}
Integer tmp = aux.element;
aux.element = element;
return tmp;
}
/**
* Esvazia a lista
*/
public void clear() {
header = new Node(null);
trailer = new Node(null);
header.next = trailer;
trailer.prev = header;
count = 0;
}
/**
* Return size
*/
public int size() {
return count;
}
/**
* Checks if list is empty
*/
public boolean isEmpty() {
return (count == 0);
}
@Override
public String toString() {
StringBuilder s = new StringBuilder();
Node aux = header.next;
for (int i = 0; i < count; i++) {
s.append(aux.element.toString());
s.append("\n");
aux = aux.next;
}
return s.toString();
}
}