._ 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.