Loquesea
/**
* @author JHOHAN FRANCO SANCHEZ, Universidad Nacional De Colombia
*/
import java.util.Iterator;
public class MyLinkedList<E> implements Iterable<E> {
private int theSize;
private int modCount = 0;
private Node<E> beginMarker;
private Node<E> endMarker;
public MyLinkedList() {
doClear();
}
public classNode<E> {
// package visible data members
public E data;
public Node<E> prev;
public Node<E> next;
public Node(E element, Node<E> n){
data = element;
next= n;
}
public Node(E element, Node<E> p, Node<E> n) {
data = element;
prev = p;
next = n;
}
}
private classLinkedListIterator implements java.util.Iterator<E> {
private Node<E> current = beginMarker.next;
private int expectedModCount = modCount;
private boolean okToRemove = false;
public boolean hasNext() {
return current != endMarker;
}
public E next() {
if (modCount != expectedModCount) {
throw newjava.util.ConcurrentModificationException();
}
if (!hasNext()) {
throw new java.util.NoSuchElementException();
}
E nextItem = current.data;
current = current.next;
okToRemove = true;
return nextItem;
}
public void remove() {
if (modCount != expectedModCount) {
throw newjava.util.ConcurrentModificationException();
}
if (!okToRemove) {
throw new IllegalStateException();
}
MyLinkedList.this.remove(current.prev);
expectedModCount++;
okToRemove = false;
}
}
public void clear() {
doClear();
}
private void doClear() {
beginMarker = new Node<E>(null, null,null);
endMarker = new Node<E>(null, beginMarker, null);
beginMarker.next = endMarker;
theSize = 0;
modCount++;
}
public int size() {
return theSize;
}
public boolean isEmpty() {
return size() == 0;
}
public boolean add(E x) {
add(size(), x);
return true;
}
public voidadd(int idx, E x) {
addBefore(getNode(idx, 0, size()), x);
}
public E get(int idx) {
return getNode(idx).data;
}
public E set(int idx, E newVal) {
Node<E> p = getNode(idx);
E oldVal = p.data;
p.data = newVal;
return oldVal;
}
public E remove(int idx) {
return remove(getNode(idx));
}private void addBefore(Node<E> p, E x) {
//Node<E> newNode = new Node( x, p.prev, p );
//p.prev.next = newNode;
//p.prev = newNode;
Node<E> newNode = new Node<E>(x, p.prev, p);
newNode.prev.next = newNode;
p.prev = newNode;
theSize++;
modCount++;
}
public E remove(Node<E> p) {p.next.prev = p.prev;
p.prev.next = p.next;
theSize--;
modCount++;
return p.data;
}
private Node<E> getNode(int idx) {
return getNode(idx, 0, size() - 1);
}
private Node<E> getNode(int idx, int lower, int upper) {
Node<E> p;
if (idx < lower || idx > upper) {
throw newIndexOutOfBoundsException();
}
if (idx < size() / 2) {
p = beginMarker.next;
for (int i = 0; i < idx; i++) {
p = p.next;
}
} else {
p = endMarker;
for (int i = size(); i > idx; i--) {
p = p.prev;
}
}
return p;
}
public...
Regístrate para leer el documento completo.