From 31405075b04d81b6f80e3caae66531399675cdba Mon Sep 17 00:00:00 2001 From: La Programmatrice Verde Date: Tue, 24 Mar 2026 09:27:40 +0100 Subject: [PATCH] removeElementAt --- src/linkedlist/Lista.java | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/linkedlist/Lista.java b/src/linkedlist/Lista.java index 78bd7fd..6670e56 100644 --- a/src/linkedlist/Lista.java +++ b/src/linkedlist/Lista.java @@ -80,13 +80,32 @@ public class Lista { } 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 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)