sur->mac
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
[ {
|
||||
"version" : "9.6.0-20260502004807+0000",
|
||||
"buildTime" : "20260502004807+0000",
|
||||
"commitId" : "b6e68b2bcae07ceceb366b4d0a900e4e93ba4c1d",
|
||||
"version" : "9.6.0-20260511004857+0000",
|
||||
"buildTime" : "20260511004857+0000",
|
||||
"commitId" : "d21d434e616b55c833aa59b433adf393010f40d8",
|
||||
"current" : false,
|
||||
"snapshot" : true,
|
||||
"nightly" : true,
|
||||
@@ -10,11 +10,62 @@
|
||||
"rcFor" : "",
|
||||
"milestoneFor" : "",
|
||||
"broken" : false,
|
||||
"downloadUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.6.0-20260502004807+0000-bin.zip",
|
||||
"checksumUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.6.0-20260502004807+0000-bin.zip.sha256",
|
||||
"checksum" : "85e34aa0a33686cfac1fb2e0ca79c6e4929be140ec8dae7956fb6b84241270b5",
|
||||
"wrapperChecksumUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.6.0-20260502004807+0000-wrapper.jar.sha256",
|
||||
"downloadUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.6.0-20260511004857+0000-bin.zip",
|
||||
"checksumUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.6.0-20260511004857+0000-bin.zip.sha256",
|
||||
"checksum" : "6c9696b888885a574308bdead2ef69761b6fac15b21ad5f0d58d26088fda1a9c",
|
||||
"wrapperChecksumUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.6.0-20260511004857+0000-wrapper.jar.sha256",
|
||||
"wrapperChecksum" : "497c8c2a7e5031f6aa847f88104aa80a93532ec32ee17bdb8d1d2f67a194a9c7"
|
||||
}, {
|
||||
"version" : "9.6.0-milestone-2",
|
||||
"buildTime" : "20260510110612+0000",
|
||||
"commitId" : "62000451ad7b25de53fa89a155ef8ecb401621bb",
|
||||
"current" : false,
|
||||
"snapshot" : false,
|
||||
"nightly" : false,
|
||||
"releaseNightly" : false,
|
||||
"activeRc" : false,
|
||||
"rcFor" : "",
|
||||
"milestoneFor" : "9.6.0",
|
||||
"broken" : false,
|
||||
"downloadUrl" : "https://services.gradle.org/distributions/gradle-9.6.0-milestone-2-bin.zip",
|
||||
"checksumUrl" : "https://services.gradle.org/distributions/gradle-9.6.0-milestone-2-bin.zip.sha256",
|
||||
"checksum" : "a67385a5e8acaed7840b5114feb76e455f6e3567e02d4414726f6fd1179753a9",
|
||||
"wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.6.0-milestone-2-wrapper.jar.sha256",
|
||||
"wrapperChecksum" : "497c8c2a7e5031f6aa847f88104aa80a93532ec32ee17bdb8d1d2f67a194a9c7"
|
||||
}, {
|
||||
"version" : "9.5.1-20260510022507+0000",
|
||||
"buildTime" : "20260510022507+0000",
|
||||
"commitId" : "fd78213f09782e62ca4957f9cfd3d90c6c3f1767",
|
||||
"current" : false,
|
||||
"snapshot" : true,
|
||||
"nightly" : false,
|
||||
"releaseNightly" : true,
|
||||
"activeRc" : false,
|
||||
"rcFor" : "",
|
||||
"milestoneFor" : "",
|
||||
"broken" : false,
|
||||
"downloadUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.5.1-20260510022507+0000-bin.zip",
|
||||
"checksumUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.5.1-20260510022507+0000-bin.zip.sha256",
|
||||
"checksum" : "31ee63072850e69db0372d24655dbef7680aee3afaec2442d6395fc7ca672fd2",
|
||||
"wrapperChecksumUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.5.1-20260510022507+0000-wrapper.jar.sha256",
|
||||
"wrapperChecksum" : "497c8c2a7e5031f6aa847f88104aa80a93532ec32ee17bdb8d1d2f67a194a9c7"
|
||||
}, {
|
||||
"version" : "8.14.5",
|
||||
"buildTime" : "20260507110329+0000",
|
||||
"commitId" : "62345becae08b13e793521816d585102fea66398",
|
||||
"current" : false,
|
||||
"snapshot" : false,
|
||||
"nightly" : false,
|
||||
"releaseNightly" : false,
|
||||
"activeRc" : false,
|
||||
"rcFor" : "",
|
||||
"milestoneFor" : "",
|
||||
"broken" : false,
|
||||
"downloadUrl" : "https://services.gradle.org/distributions/gradle-8.14.5-bin.zip",
|
||||
"checksumUrl" : "https://services.gradle.org/distributions/gradle-8.14.5-bin.zip.sha256",
|
||||
"checksum" : "6f74b601422d6d6fc4e1f9a1ab6522f642c2fdcbc15ae33ebd30ba3d7198e854",
|
||||
"wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.14.5-wrapper.jar.sha256",
|
||||
"wrapperChecksum" : "7d3a4ac4de1c32b59bc6a4eb8ecb8e612ccd0cf1ae1e99f66902da64df296172"
|
||||
}, {
|
||||
"version" : "9.5.0",
|
||||
"buildTime" : "20260428120530+0000",
|
||||
@@ -32,23 +83,6 @@
|
||||
"checksum" : "553c78f50dafcd54d65b9a444649057857469edf836431389695608536d6b746",
|
||||
"wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-wrapper.jar.sha256",
|
||||
"wrapperChecksum" : "497c8c2a7e5031f6aa847f88104aa80a93532ec32ee17bdb8d1d2f67a194a9c7"
|
||||
}, {
|
||||
"version" : "9.5.0-20260428014943+0000",
|
||||
"buildTime" : "20260428014943+0000",
|
||||
"commitId" : "3fe117d68f3907790f3809f121aa36303a9151f8",
|
||||
"current" : false,
|
||||
"snapshot" : true,
|
||||
"nightly" : false,
|
||||
"releaseNightly" : true,
|
||||
"activeRc" : false,
|
||||
"rcFor" : "",
|
||||
"milestoneFor" : "",
|
||||
"broken" : false,
|
||||
"downloadUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.5.0-20260428014943+0000-bin.zip",
|
||||
"checksumUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.5.0-20260428014943+0000-bin.zip.sha256",
|
||||
"checksum" : "95be4dddffc251cf96cb85e6b7d5b1cf9ac7aa407c12c0b1ef212d8592c98ad0",
|
||||
"wrapperChecksumUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.5.0-20260428014943+0000-wrapper.jar.sha256",
|
||||
"wrapperChecksum" : "497c8c2a7e5031f6aa847f88104aa80a93532ec32ee17bdb8d1d2f67a194a9c7"
|
||||
}, {
|
||||
"version" : "9.5.0-rc-4",
|
||||
"buildTime" : "20260423134125+0000",
|
||||
|
||||
+296
@@ -0,0 +1,296 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
Node<E> currentNode = head;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-98
@@ -1,98 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (size == 0) {
|
||||
head = tail = new Node<E>(item, null, null);
|
||||
} else {
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
}
|
||||
+414
@@ -0,0 +1,414 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
if (!hasPrevious()) throw new NoSuchElementException();
|
||||
E data = prevNode.data;
|
||||
lastReturned = prevNode;
|
||||
nextNode = prevNode;
|
||||
prevNode = prevNode.prev;
|
||||
posNext--;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
return posNext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
return posNext - 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Il metodo remove si occupa della rimozione del nodo appena restituito
|
||||
*/
|
||||
@Override
|
||||
public void remove() {
|
||||
// Se non è stato restituito alcun nodo
|
||||
if (lastReturned == null) throw new NoSuchElementException();
|
||||
// Se l'ultimo elemento ad essere stato restituito è la testa
|
||||
if (lastReturned == head) {
|
||||
removeFirst();
|
||||
nextNode = head; // Head dopo la rimozione del primo nodo è il successivo a quello appena rimosso
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se l'ultimo elemento ad essere stato restituito è la coda
|
||||
if (lastReturned == tail) {
|
||||
removeLast();
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se è un elemento generico
|
||||
Node<E> removingNode = lastReturned;
|
||||
Node<E> nNode = removingNode.next;
|
||||
Node<E> pNode = removingNode.prev;
|
||||
|
||||
pNode.next = nNode;
|
||||
nNode.prev = pNode;
|
||||
removingNode.prev = null;
|
||||
removingNode.next = null;
|
||||
|
||||
if (lastReturned == prevNode) posNext--;
|
||||
|
||||
nextNode = nNode;
|
||||
prevNode = pNode;
|
||||
size--;
|
||||
lastReturned = null;
|
||||
}
|
||||
|
||||
/*
|
||||
* Quando viene richiamata la add, l'elemento viene aggiunto a sinistra del cursore.
|
||||
* Esempio di seguito:
|
||||
* A | B
|
||||
* chiamata add(X)
|
||||
* A X | B
|
||||
* Come detto prima la X viene aggiunta a sinistra
|
||||
*/
|
||||
@Override
|
||||
public void add(E data) {
|
||||
if (data == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(data, null, null);
|
||||
// Se la lista è vuota
|
||||
if (size == 0) {
|
||||
tail = head = newNode;
|
||||
prevNode = newNode;
|
||||
size++;
|
||||
posNext = 1;
|
||||
return;
|
||||
}
|
||||
// Se il cursore è prima dell'HEAD
|
||||
if (prevNode == null) {
|
||||
addFirst();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+225
@@ -0,0 +1,225 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
-9
@@ -1,9 +0,0 @@
|
||||
package jcf_map.exercise;
|
||||
|
||||
public class BinaryTreeMap {
|
||||
/*
|
||||
* Esercizio 1 - Esame del secondo parziale 8/1/2019
|
||||
* Scrivere un metodo generico statico ContaDuplicati che conta il numero
|
||||
* di oggetti duplicati (non univoci) contenuti in un albero binario.
|
||||
*/
|
||||
}
|
||||
+273
@@ -0,0 +1,273 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+195
@@ -0,0 +1,195 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if ()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-51
@@ -1,51 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
-96
@@ -1,96 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (size == 0) {
|
||||
head = tail = new Node<E>(item, null, null);
|
||||
} else {
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-85
@@ -1,85 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (size == 0) {
|
||||
head = tail = new Node<E>(item, null, null);
|
||||
} else {
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
}
|
||||
|
||||
}
|
||||
-73
@@ -1,73 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (size == 0) {
|
||||
head = tail = new Node<E>(item, null, null);
|
||||
} else {
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
if (size == 1) {
|
||||
E tmp = head.data;
|
||||
head = tail = null;
|
||||
return tmp;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+273
@@ -0,0 +1,273 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext = 0;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+202
@@ -0,0 +1,202 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst(object);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+237
@@ -0,0 +1,237 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
}
|
||||
|
||||
}
|
||||
+393
@@ -0,0 +1,393 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
if (!hasPrevious()) throw new NoSuchElementException();
|
||||
E data = prevNode.data;
|
||||
lastReturned = prevNode;
|
||||
nextNode = prevNode;
|
||||
prevNode = prevNode.prev;
|
||||
posNext--;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
return posNext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
return posNext - 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Il metodo remove si occupa della rimozione del nodo appena restituito
|
||||
*/
|
||||
@Override
|
||||
public void remove() {
|
||||
// Se non è stato restituito alcun nodo
|
||||
if (lastReturned == null) throw new NoSuchElementException();
|
||||
// Se l'ultimo elemento ad essere stato restituito è la testa
|
||||
if (lastReturned == head) {
|
||||
removeFirst();
|
||||
nextNode = head; // Head dopo la rimozione del primo nodo è il successivo a quello appena rimosso
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se l'ultimo elemento ad essere stato restituito è la coda
|
||||
if (lastReturned == tail) {
|
||||
removeLast();
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se è un elemento generico
|
||||
Node<E> removingNode = lastReturned;
|
||||
Node<E> nNode = removingNode.next;
|
||||
Node<E> pNode = removingNode.prev;
|
||||
|
||||
pNode.next = nNode;
|
||||
nNode.prev = pNode;
|
||||
removingNode.prev = null;
|
||||
removingNode.next = null;
|
||||
|
||||
if (lastReturned == prevNode) posNext--;
|
||||
|
||||
nextNode = nNode;
|
||||
prevNode = pNode;
|
||||
size--;
|
||||
lastReturned = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(E data) {
|
||||
if (data == null) throw new NullPointerException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-28
@@ -1,28 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+386
@@ -0,0 +1,386 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
if (!hasPrevious()) throw new NoSuchElementException();
|
||||
E data = prevNode.data;
|
||||
lastReturned = prevNode;
|
||||
nextNode = prevNode;
|
||||
prevNode = prevNode.prev;
|
||||
posNext--;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
return posNext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
return posNext - 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Il metodo remove si occupa della rimozione del nodo appena restituito
|
||||
*/
|
||||
@Override
|
||||
public void remove() {
|
||||
// Se non è stato restituito alcun nodo
|
||||
if (lastReturned == null) throw new NoSuchElementException();
|
||||
// Se l'ultimo elemento ad essere stato restituito è la testa
|
||||
if (lastReturned == head) {
|
||||
removeFirst();
|
||||
nextNode = head; // Head dopo la rimozione del primo nodo è il successivo a quello appena rimosso
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se l'ultimo elemento ad essere stato restituito è la coda
|
||||
if (lastReturned == tail) {
|
||||
removeLast();
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se è un elemento generico
|
||||
Node<E> removingNode = lastReturned;
|
||||
Node<E> nNode = removingNode.next;
|
||||
Node<E> pNode = removingNode.prev;
|
||||
|
||||
pNode.next = nNode;
|
||||
nNode.prev = pNode;
|
||||
removingNode.prev = null;
|
||||
removingNode.next = null;
|
||||
size--;
|
||||
nextNode = nNode;
|
||||
prevNode = pNode;
|
||||
posNext--;
|
||||
lastReturned = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+424
@@ -0,0 +1,424 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
if (!hasPrevious()) throw new NoSuchElementException();
|
||||
E data = prevNode.data;
|
||||
lastReturned = prevNode;
|
||||
nextNode = prevNode;
|
||||
prevNode = prevNode.prev;
|
||||
posNext--;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
return posNext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
return posNext - 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Il metodo remove si occupa della rimozione del nodo appena restituito
|
||||
*/
|
||||
@Override
|
||||
public void remove() {
|
||||
// Se non è stato restituito alcun nodo
|
||||
if (lastReturned == null) throw new NoSuchElementException();
|
||||
// Se l'ultimo elemento ad essere stato restituito è la testa
|
||||
if (lastReturned == head) {
|
||||
removeFirst();
|
||||
nextNode = head; // Head dopo la rimozione del primo nodo è il successivo a quello appena rimosso
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se l'ultimo elemento ad essere stato restituito è la coda
|
||||
if (lastReturned == tail) {
|
||||
removeLast();
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se è un elemento generico
|
||||
Node<E> removingNode = lastReturned;
|
||||
Node<E> nNode = removingNode.next;
|
||||
Node<E> pNode = removingNode.prev;
|
||||
|
||||
pNode.next = nNode;
|
||||
nNode.prev = pNode;
|
||||
removingNode.prev = null;
|
||||
removingNode.next = null;
|
||||
|
||||
if (lastReturned == prevNode) posNext--;
|
||||
|
||||
nextNode = nNode;
|
||||
prevNode = pNode;
|
||||
size--;
|
||||
lastReturned = null;
|
||||
}
|
||||
|
||||
/*
|
||||
* Quando viene richiamata la add, l'elemento viene aggiunto a sinistra del cursore.
|
||||
* Esempio di seguito:
|
||||
* A | B
|
||||
* chiamata add(X)
|
||||
* A X | B
|
||||
* Come detto prima la X viene aggiunta a sinistra
|
||||
*/
|
||||
@Override
|
||||
public void add(E data) {
|
||||
if (data == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(data, null, null);
|
||||
// Se la lista è vuota
|
||||
if (size == 0) {
|
||||
tail = head = newNode;
|
||||
prevNode = newNode;
|
||||
size++;
|
||||
posNext = 1;
|
||||
return;
|
||||
}
|
||||
// Se il cursore è prima dell'HEAD
|
||||
if (prevNode == null) {
|
||||
addFirst(data);
|
||||
prevNode = head;
|
||||
posNext++;
|
||||
return;
|
||||
}
|
||||
// Se il cursore è dopo la TAIL
|
||||
if (prevNode == tail) {
|
||||
addLast(data);
|
||||
prevNode = tail;
|
||||
posNext++;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+248
@@ -0,0 +1,248 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
-134
@@ -1,134 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+203
@@ -0,0 +1,203 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode;
|
||||
// Caso in cui sia il primo
|
||||
currentNode = head;
|
||||
if (currentNode.data.equals(object)) {
|
||||
removeFirst();
|
||||
}
|
||||
// Iterazione per la ricerca dell'oggetto
|
||||
Node<E> currentNode = head;
|
||||
|
||||
while (currentNode.next != null) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-115
@@ -1,115 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
}
|
||||
+399
@@ -0,0 +1,399 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
if (!hasPrevious()) throw new NoSuchElementException();
|
||||
E data = prevNode.data;
|
||||
lastReturned = prevNode;
|
||||
nextNode = prevNode;
|
||||
prevNode = prevNode.prev;
|
||||
posNext--;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
return posNext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
return posNext - 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Il metodo remove si occupa della rimozione del nodo appena restituito
|
||||
*/
|
||||
@Override
|
||||
public void remove() {
|
||||
// Se non è stato restituito alcun nodo
|
||||
if (lastReturned == null) throw new NoSuchElementException();
|
||||
// Se l'ultimo elemento ad essere stato restituito è la testa
|
||||
if (lastReturned == head) {
|
||||
removeFirst();
|
||||
nextNode = head; // Head dopo la rimozione del primo nodo è il successivo a quello appena rimosso
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se l'ultimo elemento ad essere stato restituito è la coda
|
||||
if (lastReturned == tail) {
|
||||
removeLast();
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se è un elemento generico
|
||||
Node<E> removingNode = lastReturned;
|
||||
Node<E> nNode = removingNode.next;
|
||||
Node<E> pNode = removingNode.prev;
|
||||
|
||||
pNode.next = nNode;
|
||||
nNode.prev = pNode;
|
||||
removingNode.prev = null;
|
||||
removingNode.next = null;
|
||||
|
||||
if (lastReturned == prevNode) posNext--;
|
||||
|
||||
nextNode = nNode;
|
||||
prevNode = pNode;
|
||||
size--;
|
||||
lastReturned = null;
|
||||
}
|
||||
|
||||
/*
|
||||
* Quando viene richiamata la add, l'elemento viene aggiunto a sinistra del cursore.
|
||||
* Esempio di seguito:
|
||||
* A | B
|
||||
*/
|
||||
@Override
|
||||
public void add(E data) {
|
||||
if (data == null) throw new NullPointerException();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+367
@@ -0,0 +1,367 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
if (!hasPrevious()) throw new NoSuchElementException();
|
||||
E data = prevNode.data;
|
||||
lastReturned = prevNode;
|
||||
nextNode = prevNode;
|
||||
prevNode = prevNode.prev;
|
||||
posNext--;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
return posNext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
return posNext - 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Il metodo remove si occupa della rimozione del nodo appena restituito
|
||||
*/
|
||||
@Override
|
||||
public void remove() {
|
||||
// Se non è stato restituito alcun nodo
|
||||
if (lastReturned == null) throw new NoSuchElementException();
|
||||
// Se l'ultimo elemento ad essere stato restituito è la testa
|
||||
if (lastReturned == head) {
|
||||
removeFirst();
|
||||
|
||||
return;
|
||||
}
|
||||
// Se l'ultimo elemento ad essere stato restituito è la coda
|
||||
if (lastReturned == tail) {
|
||||
|
||||
return;
|
||||
}
|
||||
// Se è un elemento generico
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+340
@@ -0,0 +1,340 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
if (!hasPrevious()) throw new NoSuchElementException();
|
||||
E data = prevNode.data;
|
||||
lastReturned = prevNode;
|
||||
nextNode = prevNode;
|
||||
prevNode = prevNode.prev;
|
||||
posNext--;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
return posNext;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-12
@@ -1,12 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.array.List;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E>
|
||||
|
||||
}
|
||||
+334
@@ -0,0 +1,334 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
if (!hasPrevious()) throw new NoSuchElementException();
|
||||
E data = prevNode.data;
|
||||
lastReturned = prevNode;
|
||||
nextNode = prevNode;
|
||||
prevNode = prevNode.prev;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+212
@@ -0,0 +1,212 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode;
|
||||
// Caso in cui sia il primo
|
||||
currentNode = head;
|
||||
if (currentNode.data.equals(object)) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
currentNode = tail;
|
||||
if (currentNode.data.equals(object)) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
// Caso in cui non sia il primo o l'ultimo, sia quindi tra il primo e il secondo (non per forza)
|
||||
currentNode = head;
|
||||
while (currentNode.next != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+210
@@ -0,0 +1,210 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode;
|
||||
// Caso in cui sia il primo
|
||||
currentNode = head;
|
||||
if (currentNode.data.equals(object)) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
currentNode = tail;
|
||||
if (currentNode.data.equals(object)) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
// Caso in cui non sia il primo o l'ultimo, sia quindi tra il primo e il secondo (non per forza)
|
||||
Node<E> currentNode = head;
|
||||
|
||||
while (currentNode.next != null) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+408
@@ -0,0 +1,408 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
if (!hasPrevious()) throw new NoSuchElementException();
|
||||
E data = prevNode.data;
|
||||
lastReturned = prevNode;
|
||||
nextNode = prevNode;
|
||||
prevNode = prevNode.prev;
|
||||
posNext--;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
return posNext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
return posNext - 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Il metodo remove si occupa della rimozione del nodo appena restituito
|
||||
*/
|
||||
@Override
|
||||
public void remove() {
|
||||
// Se non è stato restituito alcun nodo
|
||||
if (lastReturned == null) throw new NoSuchElementException();
|
||||
// Se l'ultimo elemento ad essere stato restituito è la testa
|
||||
if (lastReturned == head) {
|
||||
removeFirst();
|
||||
nextNode = head; // Head dopo la rimozione del primo nodo è il successivo a quello appena rimosso
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se l'ultimo elemento ad essere stato restituito è la coda
|
||||
if (lastReturned == tail) {
|
||||
removeLast();
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se è un elemento generico
|
||||
Node<E> removingNode = lastReturned;
|
||||
Node<E> nNode = removingNode.next;
|
||||
Node<E> pNode = removingNode.prev;
|
||||
|
||||
pNode.next = nNode;
|
||||
nNode.prev = pNode;
|
||||
removingNode.prev = null;
|
||||
removingNode.next = null;
|
||||
|
||||
if (lastReturned == prevNode) posNext--;
|
||||
|
||||
nextNode = nNode;
|
||||
prevNode = pNode;
|
||||
size--;
|
||||
lastReturned = null;
|
||||
}
|
||||
|
||||
/*
|
||||
* Quando viene richiamata la add, l'elemento viene aggiunto a sinistra del cursore.
|
||||
* Esempio di seguito:
|
||||
* A | B
|
||||
* chiamata add(X)
|
||||
* A X | B
|
||||
* Come detto prima la X viene aggiunta a sinistra
|
||||
*/
|
||||
@Override
|
||||
public void add(E data) {
|
||||
if (data == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>();
|
||||
// Se la lista è vuota
|
||||
if (size == 0) {
|
||||
tail = head = new Node<E>(data, null, null);
|
||||
size++;
|
||||
prevNode =
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+239
@@ -0,0 +1,239 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[];
|
||||
Node<E> currentNode = head;
|
||||
}
|
||||
|
||||
}
|
||||
-113
@@ -1,113 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (size == 0) {
|
||||
head = tail = new Node<E>(item, null, null);
|
||||
} else {
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (size == 0) {
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
}
|
||||
+238
@@ -0,0 +1,238 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean removeRec(E object) {
|
||||
if ((object == null) || (head == null)) return false;
|
||||
if (head.data.equals(object)) {
|
||||
removeFirst();
|
||||
return true;
|
||||
} else return removeRec(head.next, object);
|
||||
}
|
||||
|
||||
private boolean removeRec(Node<E> current, E object) {
|
||||
if (current == null) return false;
|
||||
if (current.data.equals(object)) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+263
@@ -0,0 +1,263 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
-99
@@ -1,99 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (size == 0) {
|
||||
head = tail = new Node<E>(item, null, null);
|
||||
} else {
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
}
|
||||
-59
@@ -1,59 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (size == 0) {
|
||||
head = tail = new Node<E>(item, null, null);
|
||||
} else {
|
||||
Node<E> newNode = new Node<E>();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+368
@@ -0,0 +1,368 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
if (!hasPrevious()) throw new NoSuchElementException();
|
||||
E data = prevNode.data;
|
||||
lastReturned = prevNode;
|
||||
nextNode = prevNode;
|
||||
prevNode = prevNode.prev;
|
||||
posNext--;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
return posNext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
return posNext - 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Il metodo remove si occupa della rimozione del nodo appena restituito
|
||||
*/
|
||||
@Override
|
||||
public void remove() {
|
||||
// Se non è stato restituito alcun nodo
|
||||
if (lastReturned == null) throw new NoSuchElementException();
|
||||
// Se l'ultimo elemento ad essere stato restituito è la testa
|
||||
if (lastReturned == head) {
|
||||
Node<E> removingNode = head;
|
||||
Node<E> nextingNode = removingNode.next;
|
||||
|
||||
return;
|
||||
}
|
||||
// Se l'ultimo elemento ad essere stato restituito è la coda
|
||||
if (lastReturned == tail) {
|
||||
|
||||
return;
|
||||
}
|
||||
// Se è un elemento generico
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-22
@@ -1,22 +0,0 @@
|
||||
package jcf_map.exercise;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import binary_tree.BinaryNode;
|
||||
|
||||
public class BinaryTreeMap {
|
||||
/*
|
||||
* Esercizio 1 - Esame del secondo parziale 8/1/2019
|
||||
* Scrivere un metodo generico statico ContaDuplicati che conta il numero
|
||||
* di oggetti duplicati (non univoci) contenuti in un albero binario.
|
||||
* Il risultato è il conteggio totale degli elementi che risultano duplicati
|
||||
* (non il numero totale di occorrenze, ma il numero di oggetti distinti che
|
||||
* hanno almeno un duplicato)
|
||||
*/
|
||||
public static <T> Map<T, Integer> ContaDuplicati(BinaryNode<T> node) {
|
||||
|
||||
}
|
||||
|
||||
protected static <T>
|
||||
|
||||
}
|
||||
+212
@@ -0,0 +1,212 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode;
|
||||
// Caso in cui sia il primo
|
||||
currentNode = head;
|
||||
if (currentNode.data.equals(object)) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
currentNode = tail;
|
||||
if (currentNode.data.equals(object)) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
// Caso in cui non sia il primo o l'ultimo, sia quindi tra il primo e il secondo (non per forza)
|
||||
currentNode = head;
|
||||
while (currentNode.next != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-88
@@ -1,88 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (size == 0) {
|
||||
head = tail = new Node<E>(item, null, null);
|
||||
} else {
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
+219
@@ -0,0 +1,219 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode;
|
||||
// Caso in cui sia il primo
|
||||
currentNode = head;
|
||||
if (currentNode.data.equals(object)) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
currentNode = tail;
|
||||
if (currentNode.data.equals(object)) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
// Caso in cui non sia il primo o l'ultimo, sia quindi tra il primo e il secondo (non per forza)
|
||||
currentNode = head;
|
||||
while (currentNode.next != null) {
|
||||
currentNode = currentNode.next;
|
||||
if (currentNode.data.equals(object)) {
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
size--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+195
@@ -0,0 +1,195 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-60
@@ -1,60 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (size == 0) {
|
||||
head = tail = new Node<E>(item, null, null);
|
||||
} else {
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-20
@@ -1,20 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Data
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E>
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+207
@@ -0,0 +1,207 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode;
|
||||
// Caso in cui sia il primo
|
||||
currentNode = head;
|
||||
if (currentNode.data.equals(object)) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
currentNode = tail;
|
||||
if ()
|
||||
// Iterazione per la ricerca dell'oggetto
|
||||
Node<E> currentNode = head;
|
||||
|
||||
while (currentNode.next != null) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+287
@@ -0,0 +1,287 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
lastReturned = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+329
@@ -0,0 +1,329 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-48
@@ -1,48 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
|
||||
}
|
||||
}
|
||||
+206
@@ -0,0 +1,206 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode;
|
||||
// Caso in cui sia il primo
|
||||
currentNode = head;
|
||||
if (currentNode.data.equals(object)) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
currentNode = tail;
|
||||
// Iterazione per la ricerca dell'oggetto
|
||||
Node<E> currentNode = head;
|
||||
|
||||
while (currentNode.next != null) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+330
@@ -0,0 +1,330 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
if (!hasPrevious()) throw new NoSuchElementException();
|
||||
E data = prevNode.data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-76
@@ -1,76 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (size == 0) {
|
||||
head = tail = new Node<E>(item, null, null);
|
||||
} else {
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
head.next.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
}
|
||||
+271
@@ -0,0 +1,271 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+268
@@ -0,0 +1,268 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Metodi adoperati
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-83
@@ -1,83 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (size == 0) {
|
||||
head = tail = new Node<E>(item, null, null);
|
||||
} else {
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
+201
@@ -0,0 +1,201 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode;
|
||||
// Caso in cui sia il primo
|
||||
currentNode = head;
|
||||
if (currentNode.data.equals(object))
|
||||
// Iterazione per la ricerca dell'oggetto
|
||||
Node<E> currentNode = head;
|
||||
|
||||
while (currentNode.next != null) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-114
@@ -1,114 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
}
|
||||
+300
@@ -0,0 +1,300 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while () {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+391
@@ -0,0 +1,391 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
if (!hasPrevious()) throw new NoSuchElementException();
|
||||
E data = prevNode.data;
|
||||
lastReturned = prevNode;
|
||||
nextNode = prevNode;
|
||||
prevNode = prevNode.prev;
|
||||
posNext--;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
return posNext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
return posNext - 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Il metodo remove si occupa della rimozione del nodo appena restituito
|
||||
*/
|
||||
@Override
|
||||
public void remove() {
|
||||
// Se non è stato restituito alcun nodo
|
||||
if (lastReturned == null) throw new NoSuchElementException();
|
||||
// Se l'ultimo elemento ad essere stato restituito è la testa
|
||||
if (lastReturned == head) {
|
||||
removeFirst();
|
||||
nextNode = head; // Head dopo la rimozione del primo nodo è il successivo a quello appena rimosso
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se l'ultimo elemento ad essere stato restituito è la coda
|
||||
if (lastReturned == tail) {
|
||||
removeLast();
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se è un elemento generico
|
||||
Node<E> removingNode = lastReturned;
|
||||
Node<E> nNode = removingNode.next;
|
||||
Node<E> pNode = removingNode.prev;
|
||||
|
||||
pNode.next = nNode;
|
||||
nNode.prev = pNode;
|
||||
removingNode.prev = null;
|
||||
removingNode.next = null;
|
||||
|
||||
if (lastReturned == prevNode) posNext--;
|
||||
|
||||
nextNode = nNode;
|
||||
prevNode = pNode;
|
||||
size--;
|
||||
lastReturned = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+197
@@ -0,0 +1,197 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+244
@@ -0,0 +1,244 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i] = currentNode;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+294
@@ -0,0 +1,294 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-59
@@ -1,59 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (size == 0) {
|
||||
head = tail = new Node<E>
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-77
@@ -1,77 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (size == 0) {
|
||||
head = tail = new Node<E>(item, null, null);
|
||||
} else {
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
tmp = head.data;
|
||||
head = tail = null;
|
||||
} else {
|
||||
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
}
|
||||
-54
@@ -1,54 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
}
|
||||
|
||||
}
|
||||
+243
@@ -0,0 +1,243 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
for () {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-126
@@ -1,126 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
}
|
||||
+213
@@ -0,0 +1,213 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode;
|
||||
// Caso in cui sia il primo
|
||||
currentNode = head;
|
||||
if (currentNode.data.equals(object)) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
currentNode = tail;
|
||||
if (currentNode.data.equals(object)) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
// Caso in cui non sia il primo o l'ultimo, sia quindi tra il primo e il secondo (non per forza)
|
||||
currentNode = head;
|
||||
while (currentNode.next != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-24
@@ -1,24 +0,0 @@
|
||||
package jcf_map.exercise;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import binary_tree.BinaryNode;
|
||||
|
||||
public class BinaryTreeMap {
|
||||
/*
|
||||
* Esercizio 1 - Esame del secondo parziale 8/1/2019
|
||||
* Scrivere un metodo generico statico ContaDuplicati che conta il numero
|
||||
* di oggetti duplicati (non univoci) contenuti in un albero binario.
|
||||
* Il risultato è il conteggio totale degli elementi che risultano duplicati
|
||||
* (non il numero totale di occorrenze, ma il numero di oggetti distinti che
|
||||
* hanno almeno un duplicato)
|
||||
*/
|
||||
public static <T> Map<T, Integer> ContaDuplicati(BinaryNode<T> node) {
|
||||
|
||||
}
|
||||
|
||||
protected static <T> Map<T, Integer> ContaDuplicati(BinaryNode<T> node, HashMap<T, Integer> ) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-37
@@ -1,37 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MuLinkedList() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+239
@@ -0,0 +1,239 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
}
|
||||
|
||||
}
|
||||
+240
@@ -0,0 +1,240 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
}
|
||||
|
||||
}
|
||||
+235
@@ -0,0 +1,235 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean removeRec(E object) {
|
||||
if ((object == null) || (head == null)) return false;
|
||||
if (head.data.equals(object)) {
|
||||
removeFirst();
|
||||
return true;
|
||||
} else return removeRec(head.next, object);
|
||||
}
|
||||
|
||||
private boolean removeRec(Node<E> cur, E object) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+329
@@ -0,0 +1,329 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
if (!hasPrevious()) throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+281
@@ -0,0 +1,281 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+423
@@ -0,0 +1,423 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
if (!hasPrevious()) throw new NoSuchElementException();
|
||||
E data = prevNode.data;
|
||||
lastReturned = prevNode;
|
||||
nextNode = prevNode;
|
||||
prevNode = prevNode.prev;
|
||||
posNext--;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
return posNext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
return posNext - 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Il metodo remove si occupa della rimozione del nodo appena restituito
|
||||
*/
|
||||
@Override
|
||||
public void remove() {
|
||||
// Se non è stato restituito alcun nodo
|
||||
if (lastReturned == null) throw new NoSuchElementException();
|
||||
// Se l'ultimo elemento ad essere stato restituito è la testa
|
||||
if (lastReturned == head) {
|
||||
removeFirst();
|
||||
nextNode = head; // Head dopo la rimozione del primo nodo è il successivo a quello appena rimosso
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se l'ultimo elemento ad essere stato restituito è la coda
|
||||
if (lastReturned == tail) {
|
||||
removeLast();
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se è un elemento generico
|
||||
Node<E> removingNode = lastReturned;
|
||||
Node<E> nNode = removingNode.next;
|
||||
Node<E> pNode = removingNode.prev;
|
||||
|
||||
pNode.next = nNode;
|
||||
nNode.prev = pNode;
|
||||
removingNode.prev = null;
|
||||
removingNode.next = null;
|
||||
|
||||
if (lastReturned == prevNode) posNext--;
|
||||
|
||||
nextNode = nNode;
|
||||
prevNode = pNode;
|
||||
size--;
|
||||
lastReturned = null;
|
||||
}
|
||||
|
||||
/*
|
||||
* Quando viene richiamata la add, l'elemento viene aggiunto a sinistra del cursore.
|
||||
* Esempio di seguito:
|
||||
* A | B
|
||||
* chiamata add(X)
|
||||
* A X | B
|
||||
* Come detto prima la X viene aggiunta a sinistra
|
||||
*/
|
||||
@Override
|
||||
public void add(E data) {
|
||||
if (data == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(data, null, null);
|
||||
// Se la lista è vuota
|
||||
if (size == 0) {
|
||||
tail = head = newNode;
|
||||
prevNode = newNode;
|
||||
size++;
|
||||
posNext = 1;
|
||||
return;
|
||||
}
|
||||
// Se il cursore è prima dell'HEAD
|
||||
if (prevNode == null) {
|
||||
addFirst(data);
|
||||
prevNode = head;
|
||||
posNext++;
|
||||
return;
|
||||
}
|
||||
// Se il cursore è dopo la TAIL
|
||||
if (prevNode == tail) {
|
||||
addLast(data);
|
||||
prevNode = tail;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+276
@@ -0,0 +1,276 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode =
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+428
@@ -0,0 +1,428 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
if (!hasPrevious()) throw new NoSuchElementException();
|
||||
E data = prevNode.data;
|
||||
lastReturned = prevNode;
|
||||
nextNode = prevNode;
|
||||
prevNode = prevNode.prev;
|
||||
posNext--;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
return posNext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
return posNext - 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Il metodo remove si occupa della rimozione del nodo appena restituito
|
||||
*/
|
||||
@Override
|
||||
public void remove() {
|
||||
// Se non è stato restituito alcun nodo
|
||||
if (lastReturned == null) throw new NoSuchElementException();
|
||||
// Se l'ultimo elemento ad essere stato restituito è la testa
|
||||
if (lastReturned == head) {
|
||||
removeFirst();
|
||||
nextNode = head; // Head dopo la rimozione del primo nodo è il successivo a quello appena rimosso
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se l'ultimo elemento ad essere stato restituito è la coda
|
||||
if (lastReturned == tail) {
|
||||
removeLast();
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se è un elemento generico
|
||||
Node<E> removingNode = lastReturned;
|
||||
Node<E> nNode = removingNode.next;
|
||||
Node<E> pNode = removingNode.prev;
|
||||
|
||||
pNode.next = nNode;
|
||||
nNode.prev = pNode;
|
||||
removingNode.prev = null;
|
||||
removingNode.next = null;
|
||||
|
||||
if (lastReturned == prevNode) posNext--;
|
||||
|
||||
nextNode = nNode;
|
||||
prevNode = pNode;
|
||||
size--;
|
||||
lastReturned = null;
|
||||
}
|
||||
|
||||
/*
|
||||
* Quando viene richiamata la add, l'elemento viene aggiunto a sinistra del cursore.
|
||||
* Esempio di seguito:
|
||||
* A | B
|
||||
* chiamata add(X)
|
||||
* A X | B
|
||||
* Come detto prima la X viene aggiunta a sinistra
|
||||
*/
|
||||
@Override
|
||||
public void add(E data) {
|
||||
if (data == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(data, null, null);
|
||||
// Se la lista è vuota
|
||||
if (size == 0) {
|
||||
tail = head = newNode;
|
||||
prevNode = newNode;
|
||||
size++;
|
||||
posNext = 1;
|
||||
return;
|
||||
}
|
||||
// Se il cursore è prima dell'HEAD
|
||||
if (prevNode == null) {
|
||||
addFirst(data);
|
||||
prevNode = head;
|
||||
posNext++;
|
||||
return;
|
||||
}
|
||||
// Se il cursore è dopo la TAIL
|
||||
if (prevNode == tail) {
|
||||
addLast(data);
|
||||
prevNode = tail;
|
||||
posNext++;
|
||||
return;
|
||||
}
|
||||
// Generico
|
||||
newNode.prev = nextNode;
|
||||
newNode.next = prevNode;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-12
@@ -1,12 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
implements java.array.List;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E>
|
||||
|
||||
}
|
||||
-5
@@ -1,5 +0,0 @@
|
||||
package jcf_map.exercise;
|
||||
|
||||
public class BinaryTreeMap {
|
||||
|
||||
}
|
||||
-107
@@ -1,107 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (size == 0) {
|
||||
head = tail = new Node<E>(item, null, null);
|
||||
} else {
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
}
|
||||
|
||||
}
|
||||
+218
@@ -0,0 +1,218 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode;
|
||||
// Caso in cui sia il primo
|
||||
currentNode = head;
|
||||
if (currentNode.data.equals(object)) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
currentNode = tail;
|
||||
if (currentNode.data.equals(object)) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
// Caso in cui non sia il primo o l'ultimo, sia quindi tra il primo e il secondo (non per forza)
|
||||
currentNode = head;
|
||||
while (currentNode.next != null) {
|
||||
currentNode = currentNode.next;
|
||||
if (currentNode.data.equals(object)) {
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+211
@@ -0,0 +1,211 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-5
@@ -1,5 +0,0 @@
|
||||
package jcf_map.exercise;
|
||||
|
||||
public class BinaryTreeMap {
|
||||
|
||||
}
|
||||
+239
@@ -0,0 +1,239 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
}
|
||||
|
||||
}
|
||||
+372
@@ -0,0 +1,372 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
if (!hasPrevious()) throw new NoSuchElementException();
|
||||
E data = prevNode.data;
|
||||
lastReturned = prevNode;
|
||||
nextNode = prevNode;
|
||||
prevNode = prevNode.prev;
|
||||
posNext--;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
return posNext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
return posNext - 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Il metodo remove si occupa della rimozione del nodo appena restituito
|
||||
*/
|
||||
@Override
|
||||
public void remove() {
|
||||
// Se non è stato restituito alcun nodo
|
||||
if (lastReturned == null) throw new NoSuchElementException();
|
||||
// Se l'ultimo elemento ad essere stato restituito è la testa
|
||||
if (lastReturned == head) {
|
||||
removeFirst();
|
||||
nextNode = head; // Head dopo la rimozione del primo nodo è il successivo a quello appena rimosso
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se l'ultimo elemento ad essere stato restituito è la coda
|
||||
if (lastReturned == tail) {
|
||||
removeLast();
|
||||
nextNode = null;
|
||||
|
||||
return;
|
||||
}
|
||||
// Se è un elemento generico
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+432
@@ -0,0 +1,432 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
if (!hasPrevious()) throw new NoSuchElementException();
|
||||
E data = prevNode.data;
|
||||
lastReturned = prevNode;
|
||||
nextNode = prevNode;
|
||||
prevNode = prevNode.prev;
|
||||
posNext--;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
return posNext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
return posNext - 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Il metodo remove si occupa della rimozione del nodo appena restituito
|
||||
*/
|
||||
@Override
|
||||
public void remove() {
|
||||
// Se non è stato restituito alcun nodo
|
||||
if (lastReturned == null) throw new NoSuchElementException();
|
||||
// Se l'ultimo elemento ad essere stato restituito è la testa
|
||||
if (lastReturned == head) {
|
||||
removeFirst();
|
||||
nextNode = head; // Head dopo la rimozione del primo nodo è il successivo a quello appena rimosso
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se l'ultimo elemento ad essere stato restituito è la coda
|
||||
if (lastReturned == tail) {
|
||||
removeLast();
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se è un elemento generico
|
||||
Node<E> removingNode = lastReturned;
|
||||
Node<E> nNode = removingNode.next;
|
||||
Node<E> pNode = removingNode.prev;
|
||||
|
||||
pNode.next = nNode;
|
||||
nNode.prev = pNode;
|
||||
removingNode.prev = null;
|
||||
removingNode.next = null;
|
||||
|
||||
if (lastReturned == prevNode) posNext--;
|
||||
|
||||
nextNode = nNode;
|
||||
prevNode = pNode;
|
||||
size--;
|
||||
lastReturned = null;
|
||||
}
|
||||
|
||||
/*
|
||||
* Quando viene richiamata la add, l'elemento viene aggiunto a sinistra del cursore.
|
||||
* Esempio di seguito:
|
||||
* A | B
|
||||
* chiamata add(X)
|
||||
* A X | B
|
||||
* Come detto prima la X viene aggiunta a sinistra
|
||||
*/
|
||||
@Override
|
||||
public void add(E data) {
|
||||
if (data == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(data, null, null);
|
||||
// Se la lista è vuota
|
||||
if (size == 0) {
|
||||
tail = head = newNode;
|
||||
prevNode = newNode;
|
||||
size++;
|
||||
posNext = 1;
|
||||
return;
|
||||
}
|
||||
// Se il cursore è prima dell'HEAD
|
||||
if (prevNode == null) {
|
||||
addFirst(data);
|
||||
prevNode = head;
|
||||
posNext++;
|
||||
return;
|
||||
}
|
||||
// Se il cursore è dopo la TAIL
|
||||
if (prevNode == tail) {
|
||||
addLast(data);
|
||||
prevNode = tail;
|
||||
posNext++;
|
||||
return;
|
||||
}
|
||||
// Generico
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
prevNode.next = newNode;
|
||||
nextNode.prev = newNode;
|
||||
lastReturned = null;
|
||||
posNext++;
|
||||
size++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+256
@@ -0,0 +1,256 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+243
@@ -0,0 +1,243 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-114
@@ -1,114 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = new Node<E>(item, null, null);
|
||||
} else {
|
||||
Node<E> newNode = new Node<E>();
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
}
|
||||
-20
@@ -1,20 +0,0 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Data
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E>
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+211
@@ -0,0 +1,211 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+227
@@ -0,0 +1,227 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean removeRec(E object) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+369
@@ -0,0 +1,369 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
if (!hasPrevious()) throw new NoSuchElementException();
|
||||
E data = prevNode.data;
|
||||
lastReturned = prevNode;
|
||||
nextNode = prevNode;
|
||||
prevNode = prevNode.prev;
|
||||
posNext--;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
return posNext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
return posNext - 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Il metodo remove si occupa della rimozione del nodo appena restituito
|
||||
*/
|
||||
@Override
|
||||
public void remove() {
|
||||
// Se non è stato restituito alcun nodo
|
||||
if (lastReturned == null) throw new NoSuchElementException();
|
||||
// Se l'ultimo elemento ad essere stato restituito è la testa
|
||||
if (lastReturned == head) {
|
||||
removeFirst();
|
||||
nextNode = head; // Head dopo la rimozione del primo nodo è il successivo a quello appena rimosso
|
||||
prevNode = null;
|
||||
posNext = 0
|
||||
return;
|
||||
}
|
||||
// Se l'ultimo elemento ad essere stato restituito è la coda
|
||||
if (lastReturned == tail) {
|
||||
|
||||
return;
|
||||
}
|
||||
// Se è un elemento generico
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+301
@@ -0,0 +1,301 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+209
@@ -0,0 +1,209 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+238
@@ -0,0 +1,238 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Node<E> currentNode = head;
|
||||
}
|
||||
|
||||
}
|
||||
+244
@@ -0,0 +1,244 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+421
@@ -0,0 +1,421 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
if (!hasPrevious()) throw new NoSuchElementException();
|
||||
E data = prevNode.data;
|
||||
lastReturned = prevNode;
|
||||
nextNode = prevNode;
|
||||
prevNode = prevNode.prev;
|
||||
posNext--;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
return posNext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
return posNext - 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Il metodo remove si occupa della rimozione del nodo appena restituito
|
||||
*/
|
||||
@Override
|
||||
public void remove() {
|
||||
// Se non è stato restituito alcun nodo
|
||||
if (lastReturned == null) throw new NoSuchElementException();
|
||||
// Se l'ultimo elemento ad essere stato restituito è la testa
|
||||
if (lastReturned == head) {
|
||||
removeFirst();
|
||||
nextNode = head; // Head dopo la rimozione del primo nodo è il successivo a quello appena rimosso
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se l'ultimo elemento ad essere stato restituito è la coda
|
||||
if (lastReturned == tail) {
|
||||
removeLast();
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se è un elemento generico
|
||||
Node<E> removingNode = lastReturned;
|
||||
Node<E> nNode = removingNode.next;
|
||||
Node<E> pNode = removingNode.prev;
|
||||
|
||||
pNode.next = nNode;
|
||||
nNode.prev = pNode;
|
||||
removingNode.prev = null;
|
||||
removingNode.next = null;
|
||||
|
||||
if (lastReturned == prevNode) posNext--;
|
||||
|
||||
nextNode = nNode;
|
||||
prevNode = pNode;
|
||||
size--;
|
||||
lastReturned = null;
|
||||
}
|
||||
|
||||
/*
|
||||
* Quando viene richiamata la add, l'elemento viene aggiunto a sinistra del cursore.
|
||||
* Esempio di seguito:
|
||||
* A | B
|
||||
* chiamata add(X)
|
||||
* A X | B
|
||||
* Come detto prima la X viene aggiunta a sinistra
|
||||
*/
|
||||
@Override
|
||||
public void add(E data) {
|
||||
if (data == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(data, null, null);
|
||||
// Se la lista è vuota
|
||||
if (size == 0) {
|
||||
tail = head = newNode;
|
||||
prevNode = newNode;
|
||||
size++;
|
||||
posNext = 1;
|
||||
return;
|
||||
}
|
||||
// Se il cursore è prima dell'HEAD
|
||||
if (prevNode == null) {
|
||||
addFirst(data);
|
||||
prevNode = head;
|
||||
posNext++;
|
||||
return;
|
||||
}
|
||||
// Se il cursore è dopo la TAIL
|
||||
if (prevNode == tail) {
|
||||
addLast(data);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user