I am implementing a class that allows me to create a linked list. But at the moment of instantiating a new linked list, I miss this error:
MainListaEnlazada.java:19: error: non-static variable this can not be referenced from a static context LinkedList list = new LinkedList ();
Could someone explain to me what I did wrong?
public class MainListaEnlazada {
public static void main(String[] args){
ListaEnlazada lista = new ListaEnlazada();
}
class Nodo {
private Object valor;
private Nodo siguiente;
public Nodo(Object valor){
this.valor = valor;
this.siguiente = null;
}
public void enlazarSiguiente(Nodo n) {
this.siguiente = n;
}
public Object getValor(){
return valor;
}
public Nodo getSiguiente() {
return siguiente;
}
}
class ListaEnlazada {
private Nodo cabeza;
private int size;
ListaEnlazada(){
cabeza = null;
size = 0;
}
public void prepend(Object _element) {
if(cabeza == null){
cabeza = new Nodo(_element);
} else {
Nodo temp = cabeza;
Nodo nuevo = new Nodo(_element);
nuevo.enlazarSiguiente(temp);
cabeza = nuevo;
}
this.size++;
}
public boolean vacia(){
return (cabeza == null)?true:false;
}
public int size(){
return this.size;
}
}
}