Skip to content

Commit 61cbfc5

Browse files
committed
added linkedlist
1 parent e11898e commit 61cbfc5

File tree

1 file changed

+209
-0
lines changed

1 file changed

+209
-0
lines changed

data-structures/LinkedList.java

+209
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
package br.pucrs.eo.es;
2+
3+
4+
public class LinkedList {
5+
6+
// Classe interna Node
7+
private class Node {
8+
9+
public Integer element;
10+
public Node next;
11+
12+
public Node(Integer element) {
13+
this.element = element;
14+
next = null;
15+
}
16+
}
17+
18+
// Referência para o primeiro elemento da lista encadeada.
19+
private Node head;
20+
// Referência para o último elemento da lista encadeada.
21+
private Node tail;
22+
// Contador para a quantidade de elementos que a lista contem.
23+
private int count;
24+
25+
/**
26+
* Construtor da lista
27+
*/
28+
public LinkedList() {
29+
head = null;
30+
tail = null;
31+
count = 0;
32+
}
33+
34+
/**
35+
* Adiciona um elemento ao final da lista
36+
*
37+
* @param element elemento a ser adicionado ao final da lista
38+
*/
39+
public void add(Integer element) {
40+
Node aux = new Node(element);
41+
if (head == null) {
42+
head = aux;
43+
} else {
44+
tail.next = aux;
45+
}
46+
tail = aux;
47+
count++;
48+
}
49+
50+
/**
51+
* Retorna o elemento de uma determinada posicao da lista
52+
*
53+
* @param index a posição da lista
54+
* @return o elemento da posicao especificada
55+
* @throws IndexOutOfBoundsException se (index < 0 || index >= size())
56+
*/
57+
public Integer get(int index) {
58+
if ((index < 0) || (index >= count)) {
59+
throw new IndexOutOfBoundsException();
60+
}
61+
Node aux = head;
62+
int c = 0;
63+
while (c < index) {
64+
aux = aux.next;
65+
c++;
66+
}
67+
return (aux.element);
68+
}
69+
70+
/**
71+
* Substitui o elemento armanzenado em uma determinada posicao da lista pelo
72+
* elemento indicado
73+
*
74+
* @param index a posicao da lista
75+
* @param element o elemento a ser armazenado na lista
76+
* @return o elemento armazenado anteriormente na posicao da lista
77+
* @throws IndexOutOfBoundsException se (index < 0 || index >= size())
78+
*/
79+
public Integer set(int index, Integer element) {
80+
if ((index < 0) || (index >= count)) {
81+
throw new IndexOutOfBoundsException();
82+
}
83+
Node aux = head;
84+
for (int i = 0; i < index; i++) {
85+
aux = aux.next;
86+
}
87+
Integer tmp = aux.element;
88+
aux.element = element;
89+
return tmp;
90+
91+
}
92+
93+
/**
94+
* Retorna true se a lista nao contem elementos
95+
*
96+
* @return true se a lista nao contem elementos
97+
*/
98+
public boolean isEmpty() {
99+
return (head == null);
100+
}
101+
102+
/**
103+
* Retorna o numero de elementos da lista
104+
*
105+
* @return o numero de elementos da lista
106+
*/
107+
public int size() {
108+
return count;
109+
}
110+
111+
/**
112+
* Esvazia a lista
113+
*/
114+
public void clear() {
115+
head = null;
116+
tail = null;
117+
count = 0;
118+
}
119+
120+
/**
121+
* Remove o elemento de uma determinada posicao da lista
122+
*
123+
* @param index a posicao da lista
124+
* @return o elemento que foi removido da lista
125+
* @throws IndexOutOfBoundsException se (index < 0 || index >= size())
126+
*/
127+
public Integer removeByIndex(int index) {
128+
if (index < 0 || index >= count) {
129+
throw new IndexOutOfBoundsException();
130+
}
131+
132+
Node aux = head;
133+
if (index == 0) {
134+
if (tail == head) // se tiver apenas um elemento
135+
{
136+
tail = null;
137+
}
138+
head = head.next;
139+
count--;
140+
return aux.element;
141+
}
142+
int c = 0;
143+
while (c < index - 1) {
144+
aux = aux.next;
145+
c++;
146+
}
147+
Integer element = aux.next.element;
148+
if (tail == aux.next) {
149+
tail = aux;
150+
}
151+
aux.next = aux.next.next;
152+
count--;
153+
return element;
154+
}
155+
156+
/**
157+
* Retorna o indice da primeira ocorrencia do elemento na lista, ou -1 se a
158+
* lista nao contem o elemento
159+
*
160+
* @param element o elemento a ser buscado
161+
* @return o indice da primeira ocorrencia do elemento na lista, ou -1 se a
162+
* lista nao contem o elemento
163+
*/
164+
public int indexOf(Integer element) {
165+
int index = 0;
166+
Node aux = head;
167+
while (aux != null) {
168+
if (aux.element.equals(element)) {
169+
return (index);
170+
}
171+
aux = aux.next;
172+
index++;
173+
}
174+
return -1;
175+
}
176+
177+
/**
178+
* Retorna true se a lista contem o elemento especificado
179+
*
180+
* @param element o elemento a ser testado
181+
* @return true se a lista contem o elemento especificado
182+
*/
183+
public boolean contains(Integer element) {
184+
Node aux = head;
185+
while (aux != null) {
186+
if (aux.element.equals(element)) {
187+
return (true);
188+
}
189+
aux = aux.next;
190+
}
191+
return false;
192+
}
193+
194+
@Override
195+
public String toString() {
196+
StringBuilder s = new StringBuilder();
197+
198+
Node aux = head;
199+
200+
while (aux != null) {
201+
s.append(aux.element.toString());
202+
s.append("\n");
203+
aux = aux.next;
204+
}
205+
206+
return s.toString();
207+
}
208+
209+
}

0 commit comments

Comments
 (0)