How can I join two lists that are simply linked? JAVA

1

It's something very simple but it has given me a little headache, I am very new. The idea is to join two lists so that:

list one = 1, 3, 5

list two = 2, 4, 6

The result should be:

list three = 1, 2, 3, 4, 5, 6

I already made the code to generate the other two and the headache goes on how to join them.

I have the class ready

    public class listas {

    nodos primero;
    byte tamanio;

    public void Lista() {
        primero = null;
        tamanio = 0;
    }

    public boolean esVacia() {
        return primero == null;
    }

    public int getTamanio() {
        return tamanio;
    }

    public void agregarAlFinal(int dato){
        nodos nuevo = new nodos();        
        nuevo.setDato(dato);
        if (esVacia()) {         
            primero = nuevo;   
        } else{
            nodos aux = primero;
            while(aux.getSiguiente() != null){
                aux = aux.getSiguiente();
            }
            aux.setSiguiente(nuevo);
        }
        tamanio++;
    }


    public void MostrarLista(){
        if (!esVacia()) {
            nodos aux = primero;            
            while(aux != null){
                System.out.print(" [ " + aux.getDato() + " ] ");
                aux = aux.getSiguiente();
            }
            System.out.print("\n");
        }       
    }     
}

And the Nodes class

public class nodos {

    int dato, dato2;
    nodos siguiente;

    public void Nodo(){
        this.dato = 0;
        this.siguiente = null;
    }

    public int getDato() {
        return dato;
    }

    public void setDato(int dato) {
        this.dato = dato;
    }

    public void setDato(int dato, int dato2){
        this.dato = dato;
        this.dato2 = dato2;
    }

    public int getDato2() {
        return dato2;
    }

    public void setDato2(int dato2) {
        this.dato2 = dato2;
    }

    public nodos getSiguiente() {
        return siguiente;
    }

    public void setSiguiente(nodos siguiente) {
        this.siguiente = siguiente;
    }  
}

And the main class.

public static void main(String[] args) {

        Scanner leer = new Scanner(System.in);
        listas lista_est = new listas();
        listas lista_prg = new listas();
//        listatr3s lista_tr3s = new listatr3s();

        int est = 0, prg = 0, opc = 0;

        do{
            System.out.print("\n1. Insertar numero de estudiantes." + 
                    "\n" + "2. Insertar número de preguntas." +
                    "\n" + "3. Mostrar tamaño de listas vigentes." + 
                    "\n" + "4. Juntar las dos listas." + 
                    "\n" + "5. Darle <play> a la asignación." +
                    "\n" + "6. Salir. \n");

            System.out.print("\nOpción: ");
            opc = leer.nextByte();

            switch(opc){
                case 1:
                    System.out.print("Ingrese el número de estudiantes: ");
                    est = leer.nextByte();
                    for (byte i = 1; i <= est; i++) {
                        lista_est.agregarAlFinal(i);
                    }
                    break;
                case 2:
                    System.out.print("Ingrese el número de preguntas: ");
                    byte cant2 = leer.nextByte();
                    for (byte i = 0; i < cant2; i++) {
                        prg = (int)Math.floor(Math.random()*(cant2-1)+1);
                        lista_prg.agregarAlFinal(prg);
                    }
                    break;
                case 3:
                    System.out.print("Listado estudiantes: "); lista_est.MostrarLista();
                    System.out.print("Listado preguntas: "); lista_prg.MostrarLista();
                    break;
                case 4:
                    System.out.print("Se agrega a la tercera lista.");
//                    lista_tr3s.agregarAlFinal(lista_est, lista_prg);
                    break;
                case 5:
                    System.out.println("Listado con la asignación: ");
//                    lista_tr3s.Mostrar3Lista();
                    break;            
            }            
        }while(opc != 6);       
    }

That works perfectly well for me to generate the lists, what I understand is that I have to do another class to generate a THIRD list. What makes it difficult is to make the code to join the two lists created.

    
asked by Edward Lopez 17.03.2018 в 23:40
source

1 answer

2

Personally I would have the class list inherit the class java.util.List. Then later you create a new list and use the addAll () method

List<String> union = new ArrayList<String>();
union.addAll(listaUno);
union.addAll(listaDos);
    
answered by 18.03.2018 в 00:50