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?
– mari
@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.
– D.rosario
Then put this part of the code in your question to complete your example: https://answall.com/help/minimal-reproducible-example.
– mari