Simple Java chained list position change

Asked

Viewed 666 times

0

I have to create a method that changes position two elements into a simple chained list, but I’m having difficulties in creating this method. In this case the method receives two elements of the type per parameter int. Ex: trocaPosicao(int m, int n); Where m should go to position n and n should go to position m

Below is the Knot class:

public class No {

int dado;
No proximo;

public No(int valor) {
    dado = valor;
    proximo = null;
}

public String imprimir() {
    return String.valueOf(dado);
}

I also have a class listaDinamica which has methods of adding, printing, removing among others.

Down with the class:

public class ListaDinamica {

private No inicio;
private No fim;
private int qtd;

public ListaDinamica() {
    inicio = null;
    fim = null;
    qtd = 0;
}

public boolean isEmpty() {
    return qtd == 0;
    //return inicio == null;
}

public int size() {
    return qtd;
}

public int sizeSemQtd() {
    No aux = inicio;
    int cont = 0;
    while (aux != null) {
        cont++;
        aux = aux.proximo;
    }
    return cont;
}

public void add(int novoElemento, int posicao) {
    if (posicao >= 0 && posicao <= qtd) {
        No novoNo = new No(novoElemento);
        if (isEmpty()) {
            inicio = novoNo;
            fim = novoNo;
        } else if (posicao == 0) {
            novoNo.proximo = inicio;
            inicio = novoNo;
        } else if (posicao == qtd) {
            fim.proximo = novoNo;
            fim = novoNo;
        } else {
            No aux = inicio;
            for (int cont = 0; cont < posicao - 1; cont++) {
                aux = aux.proximo;
            }
            novoNo.proximo = aux.proximo;
            aux.proximo = novoNo;
        }
        qtd++;
    }
}

public void add(int novoElemento) {
    add(novoElemento, qtd);
}

public int remove(int posicao) {
    if (posicao >= 0 && posicao < qtd) {
        if (qtd == 1) {
            int removido = inicio.dado;
            inicio = null;
            fim = null;
            qtd--;
            return removido;
        } else if (posicao == 0) {
            int removido = inicio.dado;
            inicio = inicio.proximo;
            qtd--;
            return removido;
        } else {
            No aux = inicio;
            for (int cont = 0; cont < posicao - 1; cont++) {
                aux = aux.proximo;
            }
            No removido = aux.proximo;
            aux.proximo = removido.proximo;
            if (posicao == qtd - 1) {
                fim = aux;
            }
            qtd--;
            return removido.dado;
        }
    } else {
        return -1;
    }
}

public void set(int novoElemento, int posicao) {
    No aux = inicio;
    if (posicao >= 0 && posicao < qtd) {
        for (int cont = 0; cont < posicao; cont++) {
            aux = aux.proximo;
        }
        aux.dado = novoElemento;
    }
}

public int get(int posicao) {
    No aux = inicio;
    if (posicao >= 0 && posicao < qtd) {
        for (int cont = 0; cont < posicao; cont++) {
            aux = aux.proximo;
        }
        return aux.dado;
    } else {
        return -1;
    }
}

public boolean exist(int elemento) {
    No aux = inicio;
    while (aux != null) {
        if (aux.dado == elemento) {
            return true;
        }
        aux = aux.proximo;
    }
    return false;
}

public String imprimir() {
    String saida = "";
    for (No aux = inicio; aux != null; aux = aux.proximo) {
        saida = saida + aux.dado + ", ";
        // saida += aux.dado + ", ";
    }
    return saida;
}

public boolean removerNo(int quantidadeNo) {
    if (!isEmpty()) {
        for (int i = 0; i < quantidadeNo; i++) {
            remove(i);
        }
        return true;
    }
    return false;
}

Class I must implement

public void trocaPosicao(int m, int n){
    add(m);
    add(n);


}
  • Looking for a ready-made answer to your workout right smart guy? = D What have you done? How did you create the chained list of us? How do you go through this list?

  • @Not even it is just difficult to understand this logic even, I already researched a little about this rsrsrs.Well the list is created through methods that adds elements, which in the method I started the elements are already added. To go through the list I use a print method that uses a simple for. The question is only how to reference nodes so that they change positions.

  • Then put this part of the code in your question to complete your example: https://answall.com/help/minimal-reproducible-example.

2 answers

2

Try to do this:

public class No {

    private int dado;
    private No proximo;

    public No(int valor) {
        dado = valor;
        proximo = null;
    }

    private No posicao(int k) {
        if (k == 0) return this;
        if (k < 0 || proximo == null) throw new IllegalArgumentException();
        return proximo.posicao(k - 1);
    }

    public String imprimir() {
        return String.valueOf(dado);
    }

    public void trocaPosicao(int m, int n) {
        No a = posicao(m);
        No b = posicao(n);
        int aux = a.dado;
        a.dado = b.dado;
        b.dado = aux;
    }
}

The trick is in the method posicao(int k). This method returns the No who is k forward positions. Having this method, within the trocaPosicao, we can get the Nos that are m and n forward positions and then exchange their contents.

  • In case the class has a method that accesses by the position, I could use it to perform the exchange?

  • @D.Osario Yes, it could. But instead of returning -1 to signal error, throw exceptions (they serve exactly to signal errors).

-1

Just to make the exchange of values is simple, you create a third variable that will serve as exchange for the exchange.

trocaPosicao(int m, int n){
  int aux = m;
  m = n;
  n = aux;
}

I just don’t understand how you’re gonna fit into your class

  • That doesn’t work at all. You will only be swapping the value of local variables that will be discarded after the end of the method execution without making any changes to the linked list.

Browser other questions tagged

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