Created
June 7, 2013 21:25
-
-
Save fforbeck/5732524 to your computer and use it in GitHub Desktop.
double linked list sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ListaDuplamenteEncadeada { | |
private Pessoa primeiraPessoa; | |
private Pessoa ultimaPessoa; | |
private int totalPessoas; | |
public void insereInicio(Pessoa novaPessoa) { | |
if (primeiraPessoa == null) { | |
primeiraPessoa = novaPessoa; | |
ultimaPessoa = primeiraPessoa; | |
} else { | |
Pessoa antigaPrimeira = primeiraPessoa; | |
novaPessoa.setProxima(antigaPrimeira); | |
antigaPrimeira.setAnterior(novaPessoa); | |
primeiraPessoa = novaPessoa; | |
} | |
totalPessoas++; | |
} | |
public void insereFim(Pessoa novaPessoa) { | |
if (ultimaPessoa == null) { | |
ultimaPessoa = novaPessoa; | |
} else { | |
Pessoa antigaUltima = ultimaPessoa; | |
novaPessoa.setAnterior(antigaUltima); | |
antigaUltima.setProxima(novaPessoa); | |
ultimaPessoa = novaPessoa; | |
} | |
totalPessoas++; | |
} | |
public void insere(Pessoa novaPessoa, int posicao) { | |
if (posicao < 1 || posicao > totalPessoas) { | |
throw new IllegalArgumentException("Posição inválida"); | |
} | |
if (posicao == 1) { | |
insereInicio(novaPessoa); | |
} else if (posicao == totalPessoas) { | |
insereFim(novaPessoa); | |
} else { | |
Pessoa p = find(posicao); | |
novaPessoa.setProxima(p); | |
novaPessoa.setAnterior(p.getAnterior()); | |
p.getAnterior().setProxima(novaPessoa); | |
p.setAnterior(novaPessoa); | |
} | |
totalPessoas++; | |
} | |
public void mostrarTotalPessoas() { | |
System.out.println("O total de pessoas na lista é: " + totalPessoas + "\n"); | |
} | |
public void removeInicio() { | |
if (primeiraPessoa == null) { | |
System.out.println("Lista vazia."); | |
} else { | |
primeiraPessoa = primeiraPessoa.getProxima(); | |
primeiraPessoa.setAnterior(null); | |
} | |
totalPessoas--; | |
} | |
public void removeFim() { | |
if (ultimaPessoa == null) { | |
System.out.println("Lista vazia."); | |
} else { | |
ultimaPessoa = ultimaPessoa.getAnterior(); | |
ultimaPessoa.setProxima(null); | |
} | |
totalPessoas--; | |
} | |
public void remove(int posicao) { | |
if (posicao < 1 || posicao > totalPessoas) { | |
throw new IllegalArgumentException("Posição inválida"); | |
} | |
if (posicao == 1) { | |
removeInicio(); | |
} else if (posicao == totalPessoas) { | |
removeFim(); | |
} else { | |
Pessoa p = find(posicao); | |
Pessoa proxima = p.getProxima(); | |
proxima.setAnterior(p.getAnterior()); | |
Pessoa anterior = p.getAnterior(); | |
anterior.setProxima(p.getProxima()); | |
} | |
totalPessoas--; | |
} | |
private Pessoa find(int posicao) { | |
int countPos = 1; | |
Pessoa p = primeiraPessoa; | |
while (countPos != posicao) { | |
p = p.getProxima(); | |
countPos++; | |
} | |
return p; | |
} | |
public Pessoa buscarPessoa(String nome) { | |
Pessoa p = primeiraPessoa; | |
while (p != null && !p.getNome().equalsIgnoreCase(nome)) { | |
p = p.getProxima(); | |
} | |
if (p != null) | |
System.out.println("Pessoa encontrada: " + p.getNome() + ", " + p.getIdade() + " anos."); | |
else | |
System.out.println("Pessoa não encontrada."); | |
return p; | |
} | |
public void imprime() { | |
System.out.println(" --- Lista duplamente encadeada --- "); | |
mostrarTotalPessoas(); | |
Pessoa p = primeiraPessoa; | |
while (p != null) { | |
System.out.println(p.getNome()); | |
p = p.getProxima(); | |
} | |
System.out.println("------------------------------------"); | |
} | |
public static void main(String[] args) { | |
ListaDuplamenteEncadeada lista = new ListaDuplamenteEncadeada(); | |
lista.insereInicio(new Pessoa("A", 1)); | |
lista.insereInicio(new Pessoa("B", 2)); | |
lista.insereInicio(new Pessoa("C", 3)); | |
lista.insereFim(new Pessoa("D", 4)); | |
lista.insereFim(new Pessoa("E", 5)); | |
lista.imprime(); | |
lista.insere(new Pessoa("F", 25), 2); | |
lista.imprime(); | |
lista.remove(4); | |
lista.imprime(); | |
lista.removeInicio(); | |
lista.imprime(); | |
lista.removeFim(); | |
lista.imprime(); | |
lista.buscarPessoa("B"); | |
lista.buscarPessoa("D"); //não deve encontrar pois foi removida. | |
} | |
public void ordenar(int idade) { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Pessoa { | |
private Pessoa anterior; | |
private Pessoa proxima; | |
private String nome; | |
private int idade; | |
public Pessoa(String nome, int idade) { | |
this.nome = nome; | |
this.idade = idade; | |
} | |
public Pessoa getAnterior() { | |
return anterior; | |
} | |
public void setAnterior(Pessoa anterior) { | |
this.anterior = anterior; | |
} | |
public Pessoa getProxima() { | |
return proxima; | |
} | |
public void setProxima(Pessoa proxima) { | |
this.proxima = proxima; | |
} | |
public String getNome() { | |
return nome; | |
} | |
public int getIdade() { | |
return idade; | |
} | |
@Override | |
public int hashCode() { | |
final int prime = 31; | |
int result = 1; | |
result = prime * result + idade; | |
result = prime * result + ((nome == null) ? 0 : nome.hashCode()); | |
return result; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) | |
return true; | |
if (obj == null) | |
return false; | |
if (getClass() != obj.getClass()) | |
return false; | |
Pessoa other = (Pessoa) obj; | |
if (idade != other.idade) | |
return false; | |
if (nome == null) { | |
if (other.nome != null) | |
return false; | |
} else if (!nome.equals(other.nome)) | |
return false; | |
return true; | |
} | |
@Override | |
public String toString() { | |
return ((anterior == null) ? "" : anterior) + | |
" <- [" + nome + " - " + idade + "] -> " + | |
((proxima == null) ? "" : proxima); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment