list linked last node to become the first

0

modify the code so that when entering the nodes of the list, the last node becomes the first, the one that is first becomes the second.

package nodos;
import java.util.Scanner;
/**
*
* @author x2010s
*/
public class Nodos {
    public String nombre;

    public Nodos nodosiguiente;
    public Nodos nodosanterior;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        /* se ingresa la cantidad de nodos a crear */    
        Scanner leer= new Scanner(System.in);
        System.out.println("Digite la cantidad de nodos a ingresar");
        int n,contador=0;
        n=leer.nextInt();

         /* el dato del nodo es ingresados */
        for (int i =1; i <= n; i++){
            Nodos nodo = new Nodos();
            System.out.print("ingrese la nombre ");
            nodo.nombre=leer.next();

            /* el nodo es ceado*/
            if(contador==0){
                nodo.nodosiguiente = null;
                contador ++;
            } else {
                nodo.nodosiguiente = nodo;
                contador ++;
            }    

            /* el colocar de primero el ultimo nodo creado*/
            if (contador == n){
                nodo.nodosiguiente=nodo;
            }
            nodo.nodosanterior = null;
            for ( i =1; i<contador; i++){
                nodo.nodosanterior=nodo;
                nodo.nodosiguiente=null;
            }

            /* la lista enlazada es imprimida*/
            for ( i =1; i <= n; i++){
                System.out.println("nombre " +nodo.nombre+ "\n");
                System.out.println("apuntador " +nodo.nodosiguiente + "\n");    
            }
        }
    }
}

When I enter the amount of 3 nodes, it only asks me once for the data, when it prints the only node it prints it three times.

    
asked by ives rodriguez 24.10.2017 в 00:58
source

1 answer

0

What happens is that you have the font used to print within the for that requests the data, it would be printing every time you enter a data, it should look something like this:

for (int i =1; i <= n; i++){
        Nodos nodo = new Nodos();
        System.out.print("ingrese la nombre ");
        nodo.nombre=leer.next();
        ...
}
//aqui iria el metodo imprimir nodo...

you also have other errors, you should check your code better

    
answered by 24.10.2017 в 01:57