removeElementAt

This commit is contained in:
La Programmatrice Verde
2026-03-24 09:27:40 +01:00
parent 76fbd74556
commit 31405075b0

View File

@@ -80,13 +80,32 @@ public class Lista<E> {
} }
E ultimo = codaNuova.getNext().getInfo(); E ultimo = codaNuova.getNext().getInfo();
codaNuova.setNext(null); codaNuova.setNext(null);
this.size--;
return ultimo; return ultimo;
} else { } else {
throw new NoSuchElementException(); throw new NoSuchElementException();
} }
} }
// removeLast(): rimuove il nodo in coda public E removeElementAt(int posizione) throws IndexOutOfBoundsException {
if (posizione == 0 || this.testa == null) {
return removeFirst();
} else if (posizione == this.size - 1) {
return removeLast();
} else if (posizione < 0 || posizione >= this.size) {
throw new IndexOutOfBoundsException();
} else {
Nodo<E> corrente = this.testa;
for (int i = 0; i < posizione - 1; i++) {
corrente = corrente.getNext();
}
E elemento = corrente.getNext().getInfo();
corrente.setNext(corrente.getNext().getNext());
this.size--;
return elemento;
}
}
// removeElementAt(int pos): rimuove il nodo in coda // removeElementAt(int pos): rimuove il nodo in coda
// tipoInfo getElementAt(int pos): restituisce l'info presente nel nodo in // tipoInfo getElementAt(int pos): restituisce l'info presente nel nodo in
// posizione n (es. tipoInfo = char, tipoInfo = int) // posizione n (es. tipoInfo = char, tipoInfo = int)