diff --git a/src/linkedlist/Lista.java b/src/linkedlist/Lista.java index 9055059..468e7b4 100644 --- a/src/linkedlist/Lista.java +++ b/src/linkedlist/Lista.java @@ -1,5 +1,7 @@ package linkedlist; +import java.util.NoSuchElementException; + public class Lista { Nodo testa; @@ -41,7 +43,7 @@ public class Lista { } public void addElementAt(E info, int posizione) throws IndexOutOfBoundsException { - if (posizione == 0) { + if (posizione == 0 || this.testa == null) { addFirst(info); } else if (posizione == size - 1) { addLast(info); @@ -59,8 +61,16 @@ public class Lista { } } - // addElementAt(tipoInfo info, int pos): aggiunge un nodo nella posizione - // indicata da pos + public E removeFirst() throws NoSuchElementException { + if (this.testa != null) { + Nodo testaVecchia = this.testa; + this.testa = this.testa.getNext(); + return testaVecchia.getInfo(); + } else { + throw new NoSuchElementException(); + } + } + // removeFirst(): rimuove il nodo in testa // removeLast(): rimuove il nodo in coda // removeElementAt(int pos): rimuove il nodo in coda