linked list manually entered by keyboard

0

When creating the list manually by keyboard, I have the following inconvenience: the node variable only stores the current value. LinkedList1 node = new LinkedList1 ();

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");    

}

}

}

}

I must change the code so that the variable changes the name every time it enters the for, for example the first iteration node1, the second iteration node2, etc. what I've tried nothing has worked.

    
asked by ives rodriguez 19.10.2017 в 05:42
source

1 answer

0

._ Hi Ives.

Your code is full of errors, from the most basic of logic to the most serious as lack of theory and knowledge of object-oriented programming and how the nodes of a list are entangled.

Logical errors:

You have a FOR inside another FOR. (according to your logic, if I ask your system 10 nodes, for each one you will try to show me the whole list) The second for (which shows the list, should go outside the first). So first fill my list, then show.

Analyze this IF better

        if (contador == 0) {
            nodo.nodosiguiente = null;
            contador++;
        } else {
            nodo.nodosiguiente = nodo;
            contador++;
        }

It would be the same as saying: if my 'counter' is 0, to the variable 'number' I assign it zero, and to the variable 'number' I assign it the variable 'number'. It does not make sense what you are doing there.

POO errors:

To create a linked list you need:

Define a class NODE (example)

public class Nodo<T> {

private T dato;
private Nodo<T> siguiente;

/**
 * Constructor por defecto
 */
public Nodo(){
    siguiente=null;
 }

/**
 * Le pasamos un dato al nodo
 * @param p 
 */
public Nodo(T p){
    siguiente=null;
    dato = p;
}

/**
 * Le pasamos un dato y su siguiente nodo al nodo
 * @param t Dato a insertar
 * @param siguiente Su sisguiente nodo
 */
public Nodo(T t, Nodo<T> siguiente){
    this.siguiente=siguiente;
    dato = t;
}

public T getDato() {
    return dato;
}

public void setDato(T dato) {
    this.dato = dato;
}

public Nodo<T> getSiguiente() {
    return siguiente;
}

public void setSiguiente(Nodo<T> siguiente) {
    this.siguiente = siguiente;
}

}

Define the class List, with the NODE attribute inside, and methods like insert (), verVat (), etc ...

public class ListaEnlazada<T>{

//Atributos
private Nodo<T> primero;

/**
 * Constructor por defecto
 */
public ListaEnlazada(){
    listaVacia();
}

/**
 * Vacia la lista
 */
private void listaVacia(){
    primero = null;
}

/**
 * Indica si la lista esta vacia o no
 * @return True = esta vacia
 */
public boolean estaVacia(){
    return primero == null;
}

/**
 * Inserta un objeto al principio de la lista
 * @param t Dato insertado
 */
public void insertarPrimero(T t){
    Nodo<T> nuevo = new Nodo<>(t);

    if (!estaVacia()){
        //Sino esta vacia, el primero actual pasa a ser
        // el siguiente de nuestro nuevo nodo
        nuevo.setSiguiente(primero);
    }

    //el primero apunta al nodo nuevo
    primero=nuevo;

}

/**
 * Inserta al final de la lista un objeto
 * @param t Dato insertado
 */
public void insertarUltimo(T t){

    Nodo<T> aux = new Nodo<>(t);
    Nodo<T> rec_aux;

    if (estaVacia()) {
        insertarPrimero(t);
    }else {
        rec_aux = primero;

        //Buscamos el ultimo nodo
        while(rec_aux.getSiguiente() != null){
            rec_aux=rec_aux.getSiguiente();
        } 

        //Actualizamos el siguiente del ultimo
        rec_aux.setSiguiente(aux);
    }
}

/**
 * Quita el primer elemento de la lista
 */
public void quitarPrimero(){
    Nodo<T> aux;
    if (!estaVacia()){
        aux=primero;
        primero = primero.getSiguiente();
        aux=null; //Lo marcamos para el recolector de basura
    }
}

/**
 * Quita el ultimo elemento de la lista
 */
public void quitarUltimo(){
    Nodo<T> aux=primero;
    if(aux.getSiguiente()==null)
       //Aqi entra, si la lista tiene un elemento
       listaVacia();
    if(!estaVacia()) {
        aux=primero;

        //Buscamos el penultimo, por eso hay dos getSiguiente()
        while(aux.getSiguiente().getSiguiente() != null){
            aux=aux.getSiguiente();
        }

        //Marcamos el siguiente del antepenultimo como nulo, eliminando el ultimo
        aux.setSiguiente(null);
    }

}        

/**
 * Devuelve el último elemento de la lista
 * @return Último elemento
 */
public T devolverUltimo(){
    T elemen = null;
    Nodo<T> aux;
    if (!estaVacia()){
        aux = primero;

        //Recorremos
        while(aux.getSiguiente() != null){
            aux = aux.getSiguiente();
        }
        elemen = aux.getDato();
    }
    return elemen;
}

And finally you need a MAIN class, where you can instantiate your LIST class and ask the user to choose the number of nodes / data they want to enter.

    
answered by 19.10.2017 в 06:12