removeElementAt
This commit is contained in:
@@ -80,13 +80,32 @@ public class Lista<E> {
|
||||
}
|
||||
E ultimo = codaNuova.getNext().getInfo();
|
||||
codaNuova.setNext(null);
|
||||
this.size--;
|
||||
return ultimo;
|
||||
} else {
|
||||
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
|
||||
// tipoInfo getElementAt(int pos): restituisce l'info presente nel nodo in
|
||||
// posizione n (es. tipoInfo = char, tipoInfo = int)
|
||||
|
||||
Reference in New Issue
Block a user