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.