Help with Java Dynamic/Chained List

Asked

Viewed 662 times

0

I’m having a hard time with a Java method. To delimit the Node position, I only have the getProximo() method as follows below, so I cannot delete the Node passed as parameter, without excluding all consecutive ones.

package br.edu.unifil.comp2022.listadinamica;
/**
 * Write a description of class ListaDinamica here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ListaDinamica{
    private Node inicio;
    private Node aux;
    private int _tamanho = 0;

    public ListaDinamica(){

    } 

    public void inserir(Node node){
        if(inicio == null){
            inicio = node;
        }else{
            Node aux = inicio;
            while(aux.getProximo() != null){
                aux = aux.getProximo();
            }
            aux.setProximo(node);
        }
        aux = inicio;
    }

    public void remover(Node node){
       if(aux != node){
          while(aux.getProximo() != null && aux.getProximo() != node){
                aux = aux.getProximo();
          }
       }else{
          aux = null;
       }
        aux = inicio;
    }    

    public boolean existe(Node node){
        if(aux == null){
           return false;
        }else if(aux == node){
           return true;
        }else if(aux.getProximo() != null){
           aux = aux.getProximo();
           return existe(aux);
        } 
         aux = inicio;
         return false;
    }

    public int tamanho(){
        Node aux = inicio;
        while(aux != null){
           aux = aux.getProximo(); 
           _tamanho++; 
        }
        return _tamanho;
    }

    @Override
    public String toString(){
        String ret;
        if(inicio == null){
            ret = "[]";
        }else{
            ret = "[";
            Node aux = inicio;
            while(aux.getProximo() != null){
                ret = ret + aux + ", ";
                aux = aux.getProximo();
            }
        }
        ret = ret + "]";
        return ret;
    }
}
  • Can you put in the rest of the code? Especially getProximo(). Is there a reason for aux be a class member? If so, you cannot have a more meaningful name?

  • I’ve completed with the Node class.

No answers

Browser other questions tagged

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