Como passar duas listas simplesmente encadeada para um duplamente encadeada

0 respostas
J
package q3;

import java.util.*;

public class q3 {

    public static class LISTA {

        public int num;
        public LISTA prox;
        public LISTA ant;
    }

    public static void main(String[] args) {


        Scanner entrada = new Scanner(System.in);
        LISTA inicio1 = null;
        LISTA inicio2 = null;
        LISTA inicio3 = null;
        LISTA aux = null;
        LISTA aux2 = null;
        LISTA aux3 = null;
        LISTA fim1 = null;
        LISTA fim2 = null;
        LISTA fim3 = null;
        LISTA temp = null;


        int op;

        for (int i = 0; i <= 19; i++) {

            System.out.println("Digite o numero" + (i + 1));
            //op = entrada.nextInt();
            LISTA novo = new LISTA();
            op = i;
            novo.num = op;
            if (op % 2 == 1) {

                LISTA novo1 = new LISTA();
                novo1 = novo;
                if (novo1.num % 2 == 1) {
                    if (inicio1 == null) {
                        inicio1 = novo1;
                        fim1 = novo1;
                        novo1.prox = null;
                        fim1.prox = null;

                    } else {
                        aux = inicio1;
                        if (aux.num >= novo1.num) {
                            novo1.prox = aux;
                            fim1 = aux;
                            inicio1 = novo1;

                        } else {
                            while (aux.prox != null) {
                                if (aux.prox.num >= novo.num) {
                                    break;
                                } else {
                                    aux = aux.prox;
                                }
                            }
                            novo1.prox = aux.prox;
                            aux.prox = novo1;
                            // fim1.prox = null;

                        }

                    }
                }
            } else if (op % 2 == 0) {
                LISTA novo3 = new LISTA();
                novo3 = novo;
                if (novo3.num % 2 == 0) {
                    if (inicio2 == null) {
                        inicio2 = novo3;
                        fim2 = novo3;
                        novo3.prox = null;

                    } else {
                        aux2 = inicio2;
                        if (aux2.num >= novo3.num) {
                            novo3.prox = aux2;
                            inicio2 = novo3;
                            fim2 = aux2;
                            fim2.prox = null;
                        } else {
                            while (aux2.prox != null) {
                                if (aux2.prox.num > novo3.num) {
                                    break;
                                } else {
                                    aux2 = aux2.prox;
                                }
                            }
                            novo3.prox = aux2.prox;
                            aux2.prox = novo3;
                            //fim2.prox = null;

                        }

                    }
                }
                aux2 = null;
                novo3 = null;
            }








        }



        aux2 = inicio2;
        System.out.println("Lista Par");
        LISTA nova1 = null;

        while (aux2 != null) {
            System.out.print(aux2.num + " ");
            nova1 = aux2;
            aux2 = aux2.prox;


            if (aux2 != null) {
                nova1.prox = aux2;
            }

        }

        nova1.prox = inicio1;
        LISTA nova3 = nova1;
        nova3 = nova3.prox;
        nova1.prox = null;
        nova1 = null;


        System.out.print("\n");
        System.out.println("Lista Impar");

        aux = inicio1;

        LISTA nova = null;

        while (aux != null) {
            System.out.print(aux.num + " ");
            nova = aux;
            aux = aux.prox;

            if (aux != null) {
                nova.prox = aux;
            }
        }

        nova.prox = inicio1;
        LISTA nova2 = nova;
        nova2 = nova2.prox;
        nova.prox = null;
        nova = null;


        inicio3 = nova3;

         LISTA novo4 = new LISTA();

        while (nova2 != null) {


            if (inicio3 == null) {
                inicio3 = nova2;
                fim3 = nova2;
                nova2.prox = null;
                nova2.ant = null;
            } else {
                aux3 = inicio3;
                while (aux3 != null && nova2.num > aux3.num) {
                    aux3 = aux3.prox;
                }

                if (aux == inicio3) {
                    nova2.prox = inicio3;
                    nova2.ant = null;
                    inicio3.ant = nova2;
                    inicio3 = nova2;
                } else if (aux3 == null) {
                    fim3.prox = nova2;
                    novo4.ant = fim3;
                    fim3 = nova2;
                    fim3.prox = null;
                } else {
                   
                    nova2.prox = aux3;
                    if(aux3.ant != null){
                    aux3.ant.prox = nova2;
                    }
                    nova2.ant = aux3.ant;
                    aux3.ant = nova2;

                }


            }


            nova2= nova2.prox;

        }
        
        
        
        
        
        aux3 = inicio3;
        while(aux3 != null)
        {
            System.out.println(aux3.num);
            aux3 = aux3.prox;
        }
    }
}
Criado 18 de abril de 2013
Respostas 0
Participantes 1