beim manuellen Erstellen der Liste per Tastatur habe ich den folgenden Nachteil, die Variable nodo speichert nur den aktuellen Wert. ListaEnlazada1 nodo = neu ListaEnlazada1();verkettete Liste manuell eingegeben durch Tastatur
package lista.enlazada1;
import java.util.Scanner;
public class ListaEnlazada1 {
public String marca;
public String modelo;
public int kilometraje;
public ListaEnlazada1 nodosiguiente;
public static void main(String[] args) {
/* enter the number of nodes to be created */
Scanner leer= new Scanner(System.in);
System.out.println("Digite la cantidad de nodos a ingresar)");
int n,contador=0;
n=leer.nextInt();
/* the three data of the node is entered */
for (int i =1; i <= n; i++){
ListaEnlazada1 nodo = new ListaEnlazada1();
System.out.print("ingrese la marca ");
nodo.marca=leer.next();
System.out.print("ingrese el modelo ");
nodo.modelo=leer.next();
System.out.print("ingrese el kilometraje ");
nodo.kilometraje=leer.nextInt();
/* the node is created */
if(contador==0){
nodo.nodosiguiente = null;
contador ++;
} else {
nodo.nodosiguiente = nodo;
contador ++;
}
/* nodes are printed */
for (i =1; i <= n; i++){
System.out.println("marca " +nodo.marca+ "\n");
System.out.println("modelo " +nodo.modelo+ "\n");
System.out.println("kilometraje " +nodo.kilometraje+ "\n");
System.out.println("apuntador " +nodo.nodosiguiente + "\n");
}
}
}
}
Ich brauche den Code zu ändern, so dass die Variable den Namen zum Beispiel jedes Mal, wenn ich geben Sie die für ändert sich die erste Iteration node1, die zweite Iteration Knoten2 usw., was ich habe versucht, nichts hat funktioniert. die Übung muss