public class Lista {
private No prim;
public Lista() {
prim = null;
System.out.println("Lista criada");
}
//Inserindo no início
public void inserir(int dado) {
No novo = new No();
novo.numero = dado;
if(prim == null) {
prim = novo;
} else {
novo.ref = prim;
prim = novo;
}
}
//encontra o o No anterior
public No buscar(No tmp) {
No aux = prim;
while (aux != null) {
if (aux.ref == tmp) {
break;
} else {
aux = aux.ref;
}
}
return aux;
}
public No buscar(int numero) {
No resp = prim;
while (resp != null) {
if (resp.numero == numero) {
break;
} else {
resp = resp.ref;
}
}
return resp;
}
public void listar() {
No aux = prim;
while (aux != null) {
System.out.println("Número: " + aux.numero);
aux = aux.ref;
}
}
public boolean remover(int numero) {
boolean res = false;
No lixo = buscar(numero);
if(lixo == prim) {
prim = prim.ref;
res = true;
} else if (lixo != null) {
No anterior = buscar(lixo);
anterior.ref = lixo.ref;
res = true;
}
return res;
}
public void inserirFim(int novoDado)
{
No aux=prim;
No novo;
while(aux != null)
{
if(aux.ref==null)
{
novo=new No();
novo.numero=novoDado;
novo.ref=null;
aux.ref=novo;
}else
aux=aux.ref;
}
}
public void inserirMeio(int novoDado, int anterior)
{
No novo=new No();
novo.numero=novoDado;
No aux=buscar(anterior);
novo.ref=aux.ref;
aux.ref=novo;
}
}