Transform a simply chained list code into a dual chained java list

Asked

Viewed 45 times

-4

I would like you to help me because I am not yet very familiar with double-chained lists.

1 answer

1


You will need to add a new field to the class Celula: anterior:

public class Celula {
    int valor;
    Celula proximo;
    
    // Adicione um campo que apontará para a célula anterior
    // Esse campo faz com que a célula seja duplamente encadeada
    Celula anterior;

    public Celula (int valor) {
        this.valor = valor;
    }
}

Next, you will need to change your code every time you create or modify a cell:

  • in the method inserir:
public void inserir (int pos, int valor) {
    Celula nova = new Celula(valor);

    if (pos == 0) {
        // Como esse é o primeiro elemento dessa lista, essa célula não
        // possui nenhuma célula anterior, portanto o inicialize em `null`
        // Esse passo é opcional, visto que o campo `anterior` da classe
        // `Celula` já é inicializado em `null` por padrão
        nova.anterior = null;
        nova.proximo = primeiro;
        primeiro = nova;
    
    } else {
        Celula acessado = primeiro;
        for (int i = 0; i < pos - 1; i++) {
            acessado = acessado.proximo;
        }
 
        // Atual:
        // [acessado.anterior] <-> [acessado] <-> [acessado.proximo]
        // Objetivo:
        // [acessado.anterior] <-> [acessado] <-> [nova] <-> [acessado.proximo]

        nova.anterior = acessado;
        nova.proximo = acessado.proximo;
        if (acessado.proximo != null)
            acessado.proximo.anterior = nova;
 
        acessado.proximo = nova;
    }

    tamanho++;
}
  • in the method remover:
public void remover (int pos) {
    if (pos == 0) {
        // Atual:
        // [primeiro] <-> [primeiro.proximo]
        // Objetivo:
        // [primeiro.proximo]

        primeiro = primeiro.proximo;
        if (primeiro.proximo != null)
            primeiro.proximo.anterior = null;
    }
    else {
        Celula acessado = primeiro;
        for (int i = 0; i < pos - 1; i++) {
            acessado = acessado.proximo;
        }

        // Atual:
        // [acessado] <-> [acessado.proximo] <-> [acessado.proximo.proximo]
        // Objetivo:
        // [acessado] <-> [acessado.proximo.proximo]

        if (acessado.proximo != null && acessado.proximo.proximo != null)
            acessado.proximo.proximo.anterior = acessado;
        acessado.proximo = acessado.proximo.proximo;
    }
    tamanho--;
}

Your main can be used as follows:

public static void main(String[] args) {
    ListaEncadeada lista = new ListaEncadeada();
    lista.inserir(0, 1);
    lista.inserir(1, 2);
    lista.inserir(2, 3);
    lista.inserir(3, 0);

    System.out.println("Populando lista:");
    for (int i = 0; i < lista.tamanhoList(); i++) {
        System.out.print(lista.acessar(i));
        System.out.print(", ");
    }
    System.out.println();

    lista.remover(0);
    System.out.println("Removendo elemento da posição 0:");
    for (int i = 0; i < lista.tamanhoList(); i++) {
        System.out.print(lista.acessar(i));
        System.out.print(", ");
    }
    System.out.println();

    lista.remover(2);
    System.out.println("Removendo elemento da posição 2:");
    for (int i = 0; i < lista.tamanhoList(); i++) {
        System.out.print(lista.acessar(i));
        System.out.print(", ");
    }
    System.out.println();
}
  • @user 111 Like all operations where you manipulate cells are being done within class ListaVector (specifically in the methods inserir and remover), there will be no need to change the external code (in this case, the main).

  • Sorry, I got confused, I meant ListaEncadeada in my last comment. I don’t know if I understand your question, but your class ListaVector represents a vector (contiguous memory) and the class ListaEncadeada represents a chain list (non-contiguous memory). It makes no sense to have double-chained list implementation for vectors since their memory is contiguous. At this link you can understand the difference between the two.

  • I can, you can edit your question including your new main?

  • I edited my answer, see if you can run the code. If there is an error or something unexpected, you can add a comment to the same question or create another one.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.