sur->mac
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,6 +1,9 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
@@ -178,6 +181,325 @@ public class MyLinkedList<E> implements List<E> {
|
||||
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() {
|
||||
return new MyLinkedListIterator();
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
return new MyLinkedListIterator();
|
||||
}
|
||||
|
||||
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++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(E data) {
|
||||
if (lastReturned == null) throw new NoSuchElementException();
|
||||
if (data == null) throw new NullPointerException();
|
||||
lastReturned.data = data;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T[] toArray(T[] a) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(Collection<?> c) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(Collection<? extends E> c) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(int index, Collection<? extends E> c) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(Collection<?> c) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(Collection<?> c) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E remove(int index) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(Object o) {
|
||||
// TODO Auto-generated method stub
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int lastIndexOf(Object o) {
|
||||
// TODO Auto-generated method stub
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator<E> listIterator(int index) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<E> subList(int fromIndex, int toIndex) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user