Double chained list - Java

Asked

Viewed 1,345 times

3

I’m trying to create (I’m learning) a doubly chained list based on an exercise, in which the list will be a train with wagons. The exercise asks to create a class "wagon", where the variables "wagon name", "next wagon" and "previous wagon" are found, and to create another class "train", where the variables "head" and "Tail" are found, and the methods to add wagons, to print the wagons and another to change the order of the locomotives. My problem is how the exercise asks to create the method to add wagons. It passes as a parameter a variable that has not been declared, having as type of given the class "wagon", so I am not able to create this method. I’m going to show you what I did in the code below. Would anyone be able to give me a hint on this? Simply, I am not able to understand how I will use the parameter "vagueCriar" in the method.

In addition, the exercise calls for the creation of two trains, then two lists, and using the method to change the locomotives, to replace the locomotive of the first train with the locomotive of the second. Would you like to know, is it really possible to do this using a double-chained (or in this case, two) list? Because I’ve done enough research and I haven’t seen anything about it. I saw that it is possible to change the order of the elements of a list, but to change the elements between two lists, I am not sure.

public class Vagao {
    String nomeDoVagao;
    Wagon vagaoAnterior;
    Wagon vagaoPosterior;

    public Vagao (String nomeDoVagao){
        this.nomeDoVagao = nomeDoVagao;
        vagaoAnterior = null;
        vagaoPosterior = null;
    }
}

public class Trem {
    Vagao head, tail = null;

    //Não estou sabendo criar aqui
    public void adicionarVagao (Vagao vagaoCriar){
        Vagao novo_Vagao = new Vagao (vagaoCriar.nomeDoVagão);

        if (head == null){
            head = tail = novo_Vagao;
            head.vagaoAnterior = null;
            tail.vagaoPosterior = null;
        } else {
            tail.vagaoPosterior = novo_Vagao;
            novo_Vagao.vagaoAnterior = tail;
            tail = novo_Vagao;
            tail.vagaoPosterior = null;
        }
    }
}

1 answer

2

I’ll give you examples of how you can make this list

Double chained non-circular:

public class Trem {
   private Vagao head = null;
   private Vagao tail = null;

   public boolean tremVazio(){
      if (head == null && tail == null)
         return true;
      else
         return false;
   }

   public void adicionarInicio(Vagao novo) {

      if(tremVazio()) {
         head = novo;
         tail = novo;
      }
      else {
         novo.vagaoPosterior = head;
         head = novo;
         novo.vagaoPosterior.vagaoAnterior = head;
      }
   }

   public void adicionarFinal(Vagao novo) {

      if(tremVazio()) {
         head = novo;
         tail = novo;
      }
      else {
         novo.vagaoAnterior = tail;
         tail = novo;
         novo.vagaoAnterior.vagaoPosterior = tail;
      }
   }
    
    
   public void imprimir() {
       Vagao aux = head;
       
       while(aux != null) {
           System.out.println("Vagao: " + aux.nomeDoVagao);
           if(aux.vagaoAnterior != null)
               System.out.println("Anterior: " + aux.vagaoAnterior.nomeDoVagao);
           if(aux.vagaoPosterior != null)
               System.out.println("Posterior: " + aux.vagaoPosterior.nomeDoVagao);
           System.out.println("------------------------------------");
           aux = aux.vagaoPosterior;
       }
       
   }
}
      

Double chained circular:

public class Trem {
   private Vagao head = null;
   private Vagao tail = null;

   public boolean tremVazio(){
     if (head == null && tail == null)
        return true;
     else
        return false;
   }

   public void adicionarInicio(Vagao novo){

     if(tremVazio()){
        head = novo;
        tail = novo;
     }

     novo.vagaoPosterior = head;
     novo.vagaoAnterior = tail;
     head = novo;
     novo.vagaoPosterior.vagaoAnterior = novo;
     tail.vagaoPosterior = novo;
   }

   public void adicionarFinal(Vagao novo){

     if(tremVazio()){
        head = novo;
        tail = novo;
     }

     novo.vagaoPosterior = head;
     novo.vagaoAnterior = tail;
     tail = novo;
     novo.vagaoPosterior.vagaoAnterior = novo;
     novo.vagaoAnterior.vagaoPosterior = novo;
   }
    
    
   public void imprimir() {
       Vagao aux = head;
       
       do {
           System.out.println("Vagao: " + aux.nomeDoVagao);
           if(aux.vagaoAnterior != null)
               System.out.println("Anterior: " + aux.vagaoAnterior.nomeDoVagao);
           if(aux.vagaoPosterior != null)
               System.out.println("Posterior: " + aux.vagaoPosterior.nomeDoVagao);
           System.out.println("------------------------------------");
           aux = aux.vagaoPosterior;
       }while(aux != head);
       
   }
}

I did it in my head during class, so I may have thrown something in there. I’m leaving the code out. It is interesting you also do search methods, removal, insertion in the middle, etc.

On the change of locomotive, it does not seem to me to make sense, being the train being made only of wagons. If you know more about this issue, comment here or in charge my twitter.

Browser other questions tagged

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