jueves, 13 de agosto de 2015

Lista doblemente enlazada en java

Generar lista doblemente enlazada
1. con inserción al inicio
2. con inserción al final
3. eliminar un elemento
4. modificar un elemento
5. impresión ascendente y descendente
6. copiar los elemento a una lista simple

Código Aquí

package listadoblementeenlazada;

import java.util.Scanner;

public class ListaDoblementeEnlazada {

    static class Nodo {

        int dato;
        Nodo ant;
        Nodo sig;
    }

    static class NodoSimple {

        int dato;

        NodoSimple sig;
    }

    public static int validarNumero() {
        Scanner leer = new Scanner(System.in);
        int valorNum = 0;
        boolean numero = false;

        while (!numero) {
            try {

                String cadena = leer.nextLine();
                valorNum = Integer.parseInt(cadena);
                numero = true;
            } catch (Exception e) {
                System.out.println("Error, ingrese un numero");

            }
        }
        return valorNum;
    }

    public static void main(String[] args) {

        // Insercion al incio
        System.out.println("Ingrese el multiplo m:");
        int m = validarNumero();
        System.out.println("Ingrese el tamaño n:");
        int n = validarNumero();;
        Nodo top = null;
        for (int i = 1; i <= n; i++) {
            Nodo temp = new Nodo();
            temp.dato = i * m;
            temp.ant = null;
            if (top == null) {
                temp.sig = null;

            } else {
                temp.sig = top;
                top.ant = temp;
            }

            top = temp;
        }
        System.out.println("Impresion de lista insercion al inicio");
        //Impresion ascendente
        Nodo temp1 = top;
        while (temp1 != null) {
            System.out.println("" + temp1.dato);
            temp1 = temp1.sig;

        }
        // Insercion al final

        Nodo top1 = null;
        Nodo ultimo = null;
        Nodo temp;
        for (int i = 1; i <= n; i++) {
            temp = new Nodo();
            temp.dato = i * m;
            temp.sig = null;
            if (top1 == null) {
                temp.ant = null;
                top1 = temp;
            } else {
                ultimo.sig = temp;
                temp.ant = ultimo;
            }
            ultimo = temp;
        }
        System.out.println("Impresion de lista insercion al final");
        //Impresion ascendente
        temp1 = top1;
        while (temp1 != null) {
            System.out.println("" + temp1.dato);
            temp1 = temp1.sig;

        }
        System.out.println("Impresion al de lista insercion al inicio  ascendente");
        //Impresion ascendente
        temp1 = top;
        while (temp1 != null) {
            System.out.println("" + temp1.dato);
            temp1 = temp1.sig;

        }
        System.out.println("Impresion al de lista insercion al inicio  descendente");
        //Impresion desendente
        temp1 = top;
        while (temp1.sig != null) {

            temp1 = temp1.sig;
        }
        while (temp1 != null) {
            System.out.println("" + temp1.dato);
            temp1 = temp1.ant;
        }
        System.out.println("Ingrese el elemento a eliminar:");
        //Eliminar
        int buscar = validarNumero();
        temp1 = top;
        while (temp1.dato != buscar && temp1.sig!=null) {
            temp1 = temp1.sig;

        }
        if(temp1.sig==null  && temp1==null){
            System.out.println("No se encontro el elemento");
           
        }else{
        temp1.ant.sig = temp1.sig;
        temp1.sig.ant = temp1.ant;
        }
        System.out.println("Impresion eliminar");
        //Impresion ascendente
        temp1 = top;
        while (temp1 != null) {
            System.out.println("" + temp1.dato);
            temp1 = temp1.sig;

        }
        System.out.println("Ingrese el elemento a modificar:");
        //modificar

        buscar = validarNumero();
        temp1 = top;
        while (temp1.dato != buscar && temp1.sig!=null) {//temp1.sig==null para que no se haga un bucle infinito
            temp1 = temp1.sig;
        }if(temp1.sig==null && temp1==null){
            System.out.println("No se encontro el elemento");
           
        }else{
        System.out.println("Ingrese el numero a modificar:");
        int modificar = validarNumero();
        temp1.dato = modificar;
        }
        System.out.println("Impresion al modificar");
        //Impresion ascendente
        temp1 = top;
        while (temp1 != null) {
            System.out.println("" + temp1.dato);
            temp1 = temp1.sig;

        }
        //
        temp1=top;
        NodoSimple top2 = null;
        NodoSimple ultimo1 = null;
        NodoSimple temp2;
       
       
        while(temp1!=null){
           
        //for (int i = 1; i <= n; i++) {
            temp2 = new NodoSimple();
            temp2.dato = temp1.dato;
            temp2.sig = null;
            if (top2 == null) {
                top2 = temp2;
            } else {
                ultimo1.sig = temp2;
            }
            ultimo1 = temp2;
            temp1=temp1.sig;
        }
        System.out.println("Lista doblemente enlazada pasada a lista simple");
        temp2=top2;
        while (temp2 != null) {
            System.out.println("" + temp2.dato);
            temp2 = temp2.sig;

        }
    }


}

Corrida 

Traslación de lista simple a arreglo, pila, cola y lista circular en java

Generar una lista simple con inserción al final, de n elementos y múltiplos de m, luego pasar a: 1) un arreglo, 2) una lista circular, 3) una pila, 4) una cola.

·         Código aquí


package deberTraslacion;

import java.util.Scanner;

public class DeberTraslacion {

    public static int validarNumero() {
        Scanner leer = new Scanner(System.in);
        int valorNum = 0;
        boolean numero = false;

        while (!numero) {
            try {
                String cadena = leer.nextLine();
                valorNum = Integer.parseInt(cadena);
                numero = true;
            } catch (Exception e) {
                System.out.println("Error, ingrese un numero");
            }
        }
        return valorNum;
    }

    public static class Nodo {

        int dato;
        Nodo sig;
    }

    public static void main(String[] args) {
        boolean salir = false;
        int opMenu;
        Scanner leer = new Scanner(System.in);
        //insercion al final
        System.out.println("Generación de una lista simple con inserción al final");
        System.out.print("Ingrese n :");
        int n = validarNumero();
        System.out.print("Ingrese m :");
        int m = validarNumero();
//genero lista
        Nodo top = null;
        Nodo ultimo = top;
        for (int i = 1; i <= n; i++) {
            Nodo temp = new Nodo();
            temp.dato = i * m;
            temp.sig = null;
            if (top == null) {// primer nodo
                top = temp;
            } else {
                ultimo.sig = temp;
            }
            ultimo = temp;

        }
        Nodo temp = top;
        System.out.println("Imprimir la lista simple:");
        while (temp != null) {
            System.out.print(" "+temp.dato);
            temp = temp.sig;
        }
        System.out.println("");
        ////////////////////////////////////////////////////      
        Nodo tempO = top;
        String menuTraslacion = "\nMenu" + "\n Pasar a un:" + "\n 1.Arreglo" + "\n 2.Lista circular" + "\n 3.Pila" + "\n 4.Cola" + "\n 5.Salir";

        do {
            System.out.println(menuTraslacion);
            opMenu = validarNumero();
            switch (opMenu) {
                case 1: {
                    tempO = top;
                    int vec[] = new int[n];
                    for (int i = 0; i < n; i++) {
                        vec[i] = tempO.dato;
                        tempO = tempO.sig;//por que tiene que pasar al siguiente elemento
                    }
                    System.out.println("La lista simple pasada a un arreglo es:");
                    for (int i = 0; i < n; i++) {
                        System.out.print(" " + vec[i]);

                    }
                    System.out.println("");
                }
                break;
                case 2: {
                    tempO = top;
                    Nodo top1 = null;
                    Nodo ultimo1 = top1;

                    //insertar al inicio de la lista circular
                    for (int i = 1; i <= n; i++) {

                        Nodo temp1 = new Nodo();
                        temp1.dato = tempO.dato;
                        tempO = tempO.sig;
                        if (top1 == null) {
                            temp1.sig = temp1;
                            ultimo1 = temp1;
                        } else {
                            temp1.sig = top1;
                            ultimo1.sig = temp1;
                        }
                        top1 = temp1;
                    }
                    //Para imprimir
                    System.out.println("La lista simple pasada a una lista circular es:");

                    Nodo temp1 = top1;
                    while (temp1.sig != top1) {   //imprime lista
                        System.out.print(" " + temp1.dato);
                        temp1 = temp1.sig;
                    }
                    System.out.print(" " + temp1.dato);
                    System.out.println("");
                }
                break;
                case 3: {
                    tempO = top;
                    Nodo top2 = null;//Mi top2 sera mi pila de esta opcion
                    for (int i = 1; i <= n; i++) {
                        Nodo temp2 = new Nodo();
                        temp2.dato = tempO.dato;
                        tempO = tempO.sig;
                        if (top2 == null) {
                            temp2.sig = null;

                        } else {
                            temp2.sig = top2;

                        }
                        top2 = temp2;
                    }

                    Nodo temp2 = top2;
                    System.out.println("La lista simple pasada a una pila es:");
                    while (temp2 != null) {   //imprime lista
                        System.out.print(" " + temp2.dato);
                        temp2 = temp2.sig;
                    }
                    System.out.println("");
                }
                break;
                case 4: {
                    tempO = top;
                    Nodo top3 = tempO; // la cola es FIFO
                    //Para imprimir la cola  
                    Nodo temp3 = top3;
                    System.out.println("La lista simple pasada a una cola es:");

                    while (temp3 != null) {
                        System.out.print(" " + temp3.dato);
                        temp3 = temp3.sig;
                    }
                    System.out.println("");
                }
                break;
                case 5: {
                    salir = true;
                    System.out.println("Fin de ejecucion");
                }
                break;
                default:
                    System.out.println("Error : La opcion no existe... ");
                    break;
            }
        } while (!salir);

    }


}
Corrida

Cola en java

Generar una cola, y aplicar métodos de insertar, eliminar y modificar.
·         Código aquí


public class Colas {

    public static class Nodo {

        int dato;
        Nodo sig;
    }

    public static void main(String[] args) {
        //creando la cola de numeros pares con 5 elementos
        int n = 5;
        int m = 2;
        Nodo top = null;
        Nodo ultimo = null;
        Nodo temp;
        for (int i = 1; i <= n; i++) {
            temp = new Nodo();
            temp.dato = i * m;
            temp.sig = null;
            if (top == null) {
                top = temp;
            } else {
                ultimo.sig = temp;
            }
            ultimo = temp;
        }
        //impresion de la cola generada
        System.out.println("Impresion de la cola generada");
        temp = top;
        while (temp != null) {
            System.out.println(temp.dato);
            temp = temp.sig;
        }
        // insertar
        int num = 12;
        Nodo insertar = new Nodo();
        insertar.dato = num;
        ultimo.sig = insertar;
        ultimo = insertar;
        //impresion de la nueva cola
        System.out.println("Impresion de la cola insertado el elemento " + num);

        temp = top;
        while (temp != null) {
            System.out.println(temp.dato);
            temp = temp.sig;
        }
        //modificar
        int buscar = 6;
        int modificar = 99;
        Nodo colat = null;
        Nodo ultimot = null;
        temp = top;
        Nodo temp1;
        while (temp.dato != buscar) {
            temp1 = new Nodo();
            temp1.dato = temp.dato;
            temp1.sig = null;
            if (colat == null)//primer nodo
            {
                colat = temp1;
            } else {
                ultimot.sig = temp1;
            }
            ultimot = temp1;
            top = top.sig;
            temp = top;

        }
        top.dato = modificar;//modificar el elemento
        if (colat != null) {
            ultimot.sig = top;//junto colas
            top = colat;//ajuste punteros
            colat = null;
        }
        // impresion de la cola modificada
        System.out.println("Cola modificado el elemento " + buscar + " por " + modificar);
        temp = top;
        while (temp != null) {
            System.out.println(temp.dato);
            temp = temp.sig;
        }

        buscar = 99;

        colat = null;
        ultimot = null;
        temp = top;

        while (temp.dato != buscar) {
            temp1 = new Nodo();
            temp1.dato = temp.dato;
            temp1.sig = null;
            if (colat == null)//primer nodo
            {
                colat = temp1;
            } else {
                ultimot.sig = temp1;
            }
            ultimot = temp1;
            top = top.sig;
            temp = top;

        }
        top = top.sig;//elimino el elemento
        if (colat != null) {
            ultimot.sig = top;//junto colas
            top = colat;//ajuste punteros
            colat = null;
        }
        System.out.println("Cola eliminado el elemento " + buscar);
        temp = top;
        while (temp != null) {
            System.out.println(temp.dato);
            temp = temp.sig;
        }
    }


}

Corrida

Pila en java

Generar una pila, y aplicar métodos de insertar, eliminar y modificar.
Código aquí


package pilasejemplo;

import pilasejemplo.PilasEjemplo.pila.Nodo;

public class PilasEjemplo {

    static class pila {

        static class Nodo {

            int dato;
            Nodo sig;
        }
    }

    public static void main(String[] args) {
//Crear
        int n = 5;
        int m = 2;
        Nodo top = null;
        for (int i = 1; i <= n; i++) {
            Nodo temp = new Nodo();
            temp.dato = m * i;
            if (top == null) {
                temp.sig = null;
            } else {
                temp.sig = top;
            }
            top = temp;
        }
//imprime
        Nodo temp = top;
        System.out.println("Pila");
        while (temp != null) {
            System.out.println(temp.dato);
            temp = temp.sig;
        }
//ingresar
        int ingresar = 12;
        Nodo tempIng = new Nodo();
        tempIng.dato = ingresar;
        tempIng.sig = top;
        top = tempIng;
//imprime
        temp = top;
        System.out.println("Pila ingresada el elemento " + ingresar);
        while (temp != null) {
            System.out.println(temp.dato);
            temp = temp.sig;
        }
//Modificar
        Nodo pilat2 = null;
        int buscar2 = 8;
        int modificar = 99;
        temp = top;
        Nodo temp2 = new Nodo();

        while (temp.dato != buscar2) {
            temp2 = new Nodo();
            temp2.dato = temp.dato;//volcado
            if (pilat2 == null) {
                temp2.sig = null;
            } else {
                temp2.sig = pilat2;

            }
            pilat2 = temp2;
            top = top.sig;
            temp = top;

        }

        top.dato = modificar; // Modifico
        temp2 = pilat2;
        while (temp2 != null) {
            temp = new Nodo();
            temp.dato = temp2.dato;
            if (top == null) {
                temp.sig = null;
            } else {
                temp.sig = top;

            }
            top = temp;
            pilat2 = pilat2.sig;
            temp2 = pilat2;

        }
//imprime
        temp = top;
        System.out.println("Pila modificada el elemento " + buscar2 + " por " + modificar);
        while (temp != null) {

            System.out.println(temp.dato);
            temp = temp.sig;
        }
//Eliminar   
        Nodo pilat = null;
        int buscar = 6;

        temp = top;
        Nodo temp1 = new Nodo();

        while (temp.dato != buscar) {
            temp1 = new Nodo();
            temp1.dato = temp.dato;//volcado
            if (pilat == null) {
                temp1.sig = null;
            } else {
                temp1.sig = pilat;
            }
            pilat = temp1;
            top = top.sig;
            temp = top;

        }
        top = top.sig; // Elimino
        temp1 = pilat;
        while (temp1 != null) {
            temp = new Nodo();
            temp.dato = temp1.dato;
            if (top == null) {
                temp.sig = null;
            } else {
                temp.sig = top;
            }
            top = temp;
            pilat = pilat.sig;
            temp1 = pilat;

        }
//imprime
        temp = top;
        System.out.println("Pila eliminada el elemento " + buscar);
        while (temp != null) {

            System.out.println(temp.dato);
            temp = temp.sig;
        }
    }

}

Corrida