forked from AllAlgorithms/java
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
Latest commit
198 lines (180 loc) · 4.75 KB
/
Copy pathLinkedList.java
File metadata and controls
198 lines (180 loc) · 4.75 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
publicclassLinkedList {
privateclassNode {
publicIntegerelement;
publicNodenext;
publicNode(Integerelement) {
this.element = element;
next = null;
}
}
privateNodehead;
privateNodetail;
privateintcount;
/**
* Construtor da lista
*/
publicLinkedList() {
head = null;
tail = null;
count = 0;
}
/**
* Adiciona um elemento ao final da lista
*
* @param element elemento a ser adicionado ao final da lista
*/
publicvoidadd(Integerelement) {
Nodeaux = newNode(element);
if (head == null) {
head = aux;
} else {
tail.next = aux;
}
tail = aux;
count++;
}
/**
* Retorna o elemento de uma determinada posicao da lista
*
* @param index a posição da lista
* @return o elemento da posicao especificada
* @throws IndexOutOfBoundsException se (index < 0 || index >= size())
*/
publicIntegerget(intindex) {
if ((index < 0) || (index >= count)) {
thrownewIndexOutOfBoundsException();
}
Nodeaux = head;
intc = 0;
while (c < index) {
aux = aux.next;
c++;
}
return (aux.element);
}
/**
* Substitui o elemento armanzenado em uma determinada posicao da lista pelo
* elemento indicado
*
* @param index a posicao da lista
* @param element o elemento a ser armazenado na lista
* @return o elemento armazenado anteriormente na posicao da lista
* @throws IndexOutOfBoundsException se (index < 0 || index >= size())
*/
publicIntegerset(intindex, Integerelement) {
if ((index < 0) || (index >= count)) {
thrownewIndexOutOfBoundsException();
}
Nodeaux = head;
for (inti = 0; i < index; i++) {
aux = aux.next;
}
Integertmp = aux.element;
aux.element = element;
returntmp;
}
/**
* Retorna true se a lista nao contem elementos
*
* @return true se a lista nao contem elementos
*/
publicbooleanisEmpty() {
return (head == null);
}
/**
* Retorna o numero de elementos da lista
*
* @return o numero de elementos da lista
*/
publicintsize() {
returncount;
}
/**
* Esvazia a lista
*/
publicvoidclear() {
head = null;
tail = null;
count = 0;
}
/**
* Remove o elemento de uma determinada posicao da lista
*
* @param index a posicao da lista
* @return o elemento que foi removido da lista
* @throws IndexOutOfBoundsException se (index < 0 || index >= size())
*/
publicIntegerremoveByIndex(intindex) {
if (index < 0 || index >= count) {
thrownewIndexOutOfBoundsException();
}
Nodeaux = head;
if (index == 0) {
if (tail == head) {
tail = null;
}
head = head.next;
count--;
returnaux.element;
}
intc = 0;
while (c < index - 1) {
aux = aux.next;
c++;
}
Integerelement = aux.next.element;
if (tail == aux.next) {
tail = aux;
}
aux.next = aux.next.next;
count--;
returnelement;
}
/**
* Retorna o indice da primeira ocorrencia do elemento na lista, ou -1 se a
* lista nao contem o elemento
*
* @param element o elemento a ser buscado
* @return o indice da primeira ocorrencia do elemento na lista, ou -1 se a
* lista nao contem o elemento
*/
publicintindexOf(Integerelement) {
intindex = 0;
Nodeaux = head;
while (aux != null) {
if (aux.element.equals(element)) {
return (index);
}
aux = aux.next;
index++;
}
return -1;
}
/**
* Retorna true se a lista contem o elemento especificado
*
* @param element o elemento a ser testado
* @return true se a lista contem o elemento especificado
*/
publicbooleancontains(Integerelement) {
Nodeaux = head;
while (aux != null) {
if (aux.element.equals(element)) {
return (true);
}
aux = aux.next;
}
returnfalse;
}
@Override
publicStringtoString() {
StringBuilders = newStringBuilder();
Nodeaux = head;
while (aux != null) {
s.append(aux.element.toString());
s.append("\n");
aux = aux.next;
}
returns.toString();
}
}