137 lines
4.1 KiB
Java
137 lines
4.1 KiB
Java
package linkedlist;
|
|
|
|
import java.util.NoSuchElementException;
|
|
|
|
public class Lista<E> {
|
|
|
|
private Nodo<E> testa;
|
|
private int size = 0;
|
|
|
|
public Lista() {
|
|
this.testa = null;
|
|
}
|
|
|
|
public int getSize() {
|
|
return this.size;
|
|
}
|
|
|
|
public void addFirst(E info) {
|
|
if (this.testa == null) {
|
|
this.testa = new Nodo<>(info);
|
|
this.size++;
|
|
} else {
|
|
Nodo<E> nuovaTesta = new Nodo<>(info);
|
|
nuovaTesta.setNext(this.testa);
|
|
this.testa = nuovaTesta;
|
|
this.size++;
|
|
}
|
|
}
|
|
|
|
public void addLast(E info) {
|
|
if (this.testa == null) {
|
|
addFirst(info);
|
|
} else {
|
|
Nodo<E> nuovaCoda = new Nodo<>(info);
|
|
|
|
Nodo<E> coda = this.testa;
|
|
while (coda.getNext() != null) {
|
|
coda = coda.getNext();
|
|
}
|
|
coda.setNext(nuovaCoda);
|
|
this.size++;
|
|
}
|
|
}
|
|
|
|
public void addElementAt(E info, int posizione) throws IndexOutOfBoundsException {
|
|
if (posizione == 0 || this.testa == null) {
|
|
addFirst(info);
|
|
} else if (posizione == this.size - 1) {
|
|
addLast(info);
|
|
} else if (posizione < 0 || posizione >= this.size) {
|
|
throw new IndexOutOfBoundsException();
|
|
} else {
|
|
Nodo<E> corrente = this.testa;
|
|
for (int i = 0; i < posizione; i++) {
|
|
corrente = corrente.getNext();
|
|
}
|
|
Nodo<E> nuovoNodo = new Nodo<>(info);
|
|
nuovoNodo.setNext(corrente.getNext());
|
|
corrente.setNext(nuovoNodo);
|
|
this.size++;
|
|
}
|
|
}
|
|
|
|
public E removeFirst() throws NoSuchElementException {
|
|
if (this.testa != null) {
|
|
Nodo<E> testaVecchia = this.testa;
|
|
this.testa = this.testa.getNext();
|
|
this.size--;
|
|
return testaVecchia.getInfo();
|
|
} else {
|
|
throw new NoSuchElementException();
|
|
}
|
|
}
|
|
|
|
public E removeLast() throws NoSuchElementException {
|
|
if (this.testa != null) {
|
|
Nodo<E> codaNuova = this.testa;
|
|
for (int i = 0; i < this.size - 2; i++) {
|
|
codaNuova = codaNuova.getNext();
|
|
}
|
|
E ultimo = codaNuova.getNext().getInfo();
|
|
codaNuova.setNext(null);
|
|
this.size--;
|
|
return ultimo;
|
|
} else {
|
|
throw new NoSuchElementException();
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
public E getElementAt(int posizione) throws IndexOutOfBoundsException, NoSuchElementException {
|
|
if (posizione == 0) {
|
|
if (this.testa == null) {
|
|
throw new NoSuchElementException();
|
|
} else {
|
|
return this.testa.getInfo();
|
|
}
|
|
} else if (posizione == this.size - 1) {
|
|
Nodo<E> coda = this.testa;
|
|
while (coda.getNext() != null) {
|
|
coda = coda.getNext();
|
|
}
|
|
return coda.getInfo();
|
|
} else if (posizione < 0 || posizione >= this.size) {
|
|
throw new IndexOutOfBoundsException();
|
|
} else {
|
|
Nodo<E> corrente = this.testa;
|
|
for (int i = 0; i < posizione; i++) {
|
|
corrente = corrente.getNext();
|
|
}
|
|
return corrente.getInfo();
|
|
}
|
|
}
|
|
|
|
// tipoInfo getElementAt(int pos): restituisce l'info presente nel nodo in
|
|
// posizione n (es. tipoInfo = char, tipoInfo = int)
|
|
// String toString()
|
|
}
|