Boa noite pessoal
Eu preciso inserir em ordem no código a seguir mas não consigo fazer em ordem
[code]public class Elemento {
int dado;
Elemento proximo;
public Elemento(int novoValor) {
this.dado = novoValor;
}
}[/code]
Teste
public class TestaListaLigadaOrdenada {
public static void main(String[] args) {
ListaLigadaOrdenada lista = new ListaLigadaOrdenada();
String ret1 = lista.imprime();
if (ret1 == null || !ret1.equals("")) {
System.err.println("Erro no teste 1. Esperado: \"\". Obtido: '" + ret1 + "'.");
System.exit(0);
}
boolean ret2 = lista.inserirEmOrdem(10);
if (!ret2) {
System.err.println("Erro no teste 2. Esperado: true. Obtido: false.");
System.exit(0);
}
lista.inserirEmOrdem(5);
lista.inserirEmOrdem(2);
String ret3 = lista.imprime();
if (ret3 == null || !ret3.equals("2,5,10,")) {
System.err.println("Erro no teste 3. Esperado: '2,5,10,'. Obtido: '" + ret3 + "'.");
System.exit(0);
}
lista.inserirEmOrdem(25);
lista.inserirEmOrdem(15);
String ret4 = lista.imprime();
if (ret4 == null || !ret4.equals("2,5,10,15,25,")) {
System.err.println("Erro no teste 4. Esperado: '2,5,10,15,25,'. Obtido: '" + ret4 + "'.");
System.exit(0);
}
lista.inserirEmOrdem(13);
lista.inserirEmOrdem(2);
String ret5 = lista.imprime();
if (ret5 == null || !ret5.equals("2,2,5,10,13,15,25,")) {
System.err.println("Erro no teste 5. Esperado: '2,2,5,10,13,15,25,'. Obtido: '" + ret5 + "'.");
System.exit(0);
}
System.out.println("Todos os testes foram executados com sucesso!");
}
}
Implementação…
Alguém sabe como fazer para inserir em ordem… ???
public class ListaLigadaOrdenada {
Elemento nohCabeca;
int tamanho = 0;
boolean inserirEmOrdem(int valor)
{
// TODO: Aqui vem a sua implementacao, incluindo o valor retornado pelo metodo
return true;
}
String imprime() {
String valores = "";
Elemento percorre = nohCabeca;
while (percorre != null) {
valores += percorre.dado + ",";
percorre = percorre.proximo;
}
return valores;
}
}