Double-threaded list – Java

Question:

I am trying to create (I am learning) a doubly linked list based on an exercise, where the list will be a train with carriages. The exercise asks you to create a "car" class, where the variables "car name", "next car" and "previous car" are found, and to create another "train" class, where the "head" variables are found " and "tail", and the methods to add cars, to print the cars and another to change the order of the locomotives. My problem is how the exercise asks to create the method to add cars. It passes as a parameter a variable that was not declared, having the class "wagon" as its data type, so I'm not able to create this method. I'll show you what I did in the code below. Would anyone be able to give me a hint about this? Simply, I'm not able to understand how I'm going to use the "vagaoCrear" parameter in the method.

In addition, the exercise asks you to create two trains, then two lists, and using the Swap Locomotives method, swap the locomotive of the first train with the locomotive of the second. I would like to know, is this really possible to accomplish using a doubly linked list (or in this case, two)? Because I've done a lot of research and I haven't seen anything about it. I saw that it's possible to change the order of elements in a list, but swapping elements between two lists, I'm 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;
        }
    }
}

Answer:

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 might have cut something there. I'm leaving the code free for editing. It's interesting that you also do search methods, removal, insertion in the middle, etc.

About the change of locomotive, it doesn't seem to make sense to me, as the train is only made of wagons. If you know more about this issue, comment here or send it on my twitter .

Scroll to Top