package dataStructures.list;
public class AnyType {
}
package dataStructures.list;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class MyLinkedList implements Iterable<AnyType>{
private static class Node<AnyType>{
public AnyType data;
public Node<AnyType> prev;
public Node<AnyType> next;
public Node(AnyType d,Node<AnyType> p,Node<AnyType> n) {
data=d;
prev=p;
next=n;
}
}
private int theSize;
private int modCount=0;
private Node<AnyType> beginMarker;
private Node<AnyType> endMarker;
public MyLinkedList() {
doClear();
}
public void clear() {
doClear();
}
public void doClear() {
beginMarker=new Node<AnyType>(null, null, null);
endMarker=new Node<AnyType>(null, beginMarker, null);
beginMarker.next=endMarker;
theSize=0;
modCount++;
}
public int size() {
return theSize;
}
public boolean isEmpty() {
return 0==size();
}
public boolean add(AnyType x) {
add(size(),x);
return true;
}
public void add(int idx,AnyType x) {
addBefore(getNode(idx), x);
}
public AnyType get(int idx) {
return getNode(idx).data;
}
public AnyType set(int idx,AnyType newVal) {
Node<AnyType> p=getNode(idx);
AnyType oldVal=p.data;
p.data=newVal;
return oldVal;
}
public AnyType remove(int idx) {
return remove(getNode(idx));
}
public void addBefore(Node<AnyType> p,AnyType x) {
Node<AnyType> newNode =new Node<>(x,p.prev,p);
newNode.prev.next=newNode;
p.prev=newNode;
theSize++;
modCount++;
}
private Node<AnyType> getNode(int idx){
return getNode(idx,0,size());
}
private Node<AnyType> getNode(int idx,int lower,int uper){
if (idx<lower || idx>uper) {
throw new IndexOutOfBoundsException();
}
Node<AnyType> p;
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;
}
private AnyType remove(Node<AnyType> p) {
p.next.prev=p.prev;
p.prev.next=p.next;
theSize--;
modCount++;
return p.data;
}
@Override
public Iterator<AnyType> iterator() {
return new LinkedListIterator();
}
private class LinkedListIterator implements Iterator<AnyType>{
private Node<AnyType> current=beginMarker.next;
private int expectedModCount=modCount;
private boolean okToRemove=false;
@Override
public boolean hasNext() {
return current!=endMarker;
}
@Override
public AnyType next() {
if (modCount!=expectedModCount) {
throw new ConcurrentModificationException();
}
if (!hasNext()) {
throw new NoSuchElementException();
}
AnyType nextItem=current.data;
current=current.next;
okToRemove=true;
return nextItem;
}
public void remove() {
if (modCount!=expectedModCount) {
throw new ConcurrentModificationException();
}
if (!okToRemove) {
throw new IllegalStateException();
}
MyLinkedList.this.remove(current.prev);
expectedModCount++;
okToRemove=false;
}
}
public static void main(String[] args) {
MyLinkedList list=new MyLinkedList();
System.out.println(list.isEmpty());
AnyType anyType1=new AnyType();
AnyType anyType2=new AnyType();
AnyType anyType3=new AnyType();
AnyType anyType4=new AnyType();
AnyType anyType5=new AnyType();
AnyType anyType6=new AnyType();
list.add(anyType1);
list.add(anyType2);
list.add(anyType3);
list.add(anyType4);
list.add(anyType5);
list.add(anyType6);
System.out.println("list的长度为:"+list.size());
list.remove(3);
System.out.println("list的长度为:"+list.size());
Iterator<AnyType> iterator=list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}