FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java/algorithms/data-structures/LinkedList.java at master · AllAlgorithms/java · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
AllAlgorithms
/
java
Public archive
Notifications
You must be signed in to change notification settings
Fork
83
Star
116
Code
Issues
3
Pull requests
6
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
java
/
algorithms
/
data-structures
/
LinkedList.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
198 lines (180 loc) · 4.75 KB
Breadcrumbs
java
/
algorithms
/
data-structures
/
LinkedList.java
Copy path
File metadata and controls
198 lines (180 loc) · 4.75 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
public
class
LinkedList
{
private
class
Node
{
public
Integer
element
;
public
Node
next
;
public
Node
(
Integer
element
) {
this
.
element
=
element
;
next
=
null
;
}
}
private
Node
head
;
private
Node
tail
;
private
int
count
;
/**
* Construtor da lista
*/
public
LinkedList
() {
head
=
null
;
tail
=
null
;
count
=
0
;
}
/**
* Adiciona um elemento ao final da lista
*
* @param element elemento a ser adicionado ao final da lista
*/
public
void
add
(
Integer
element
) {
Node
aux
=
new
Node
(
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())
*/
public
Integer
get
(
int
index
) {
if
((
index
<
0
) || (
index
>=
count
)) {
throw
new
IndexOutOfBoundsException
();
}
Node
aux
=
head
;
int
c
=
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())
*/
public
Integer
set
(
int
index
,
Integer
element
) {
if
((
index
<
0
) || (
index
>=
count
)) {
throw
new
IndexOutOfBoundsException
();
}
Node
aux
=
head
;
for
(
int
i
=
0
;
i
<
index
;
i
++) {
aux
=
aux
.
next
;
}
Integer
tmp
=
aux
.
element
;
aux
.
element
=
element
;
return
tmp
;
}
/**
* Retorna true se a lista nao contem elementos
*
* @return true se a lista nao contem elementos
*/
public
boolean
isEmpty
() {
return
(
head
==
null
);
}
/**
* Retorna o numero de elementos da lista
*
* @return o numero de elementos da lista
*/
public
int
size
() {
return
count
;
}
/**
* Esvazia a lista
*/
public
void
clear
() {
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())
*/
public
Integer
removeByIndex
(
int
index
) {
if
(
index
<
0
||
index
>=
count
) {
throw
new
IndexOutOfBoundsException
();
}
Node
aux
=
head
;
if
(
index
==
0
) {
if
(
tail
==
head
) {
tail
=
null
;
}
head
=
head
.
next
;
count
--;
return
aux
.
element
;
}
int
c
=
0
;
while
(
c
<
index
-
1
) {
aux
=
aux
.
next
;
c
++;
}
Integer
element
=
aux
.
next
.
element
;
if
(
tail
==
aux
.
next
) {
tail
=
aux
;
}
aux
.
next
=
aux
.
next
.
next
;
count
--;
return
element
;
}
/**
* 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
*/
public
int
indexOf
(
Integer
element
) {
int
index
=
0
;
Node
aux
=
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
*/
public
boolean
contains
(
Integer
element
) {
Node
aux
=
head
;
while
(
aux
!=
null
) {
if
(
aux
.
element
.
equals
(
element
)) {
return
(
true
);
}
aux
=
aux
.
next
;
}
return
false
;
}
@
Override
public
String
toString
() {
StringBuilder
s
=
new
StringBuilder
();
Node
aux
=
head
;
while
(
aux
!=
null
) {
s
.
append
(
aux
.
element
.
toString
());
s
.
append
(
"
\n
"
);
aux
=
aux
.
next
;
}
return
s
.
toString
();
}
}
Back
|
FazBrowse Home
|
New Git URL