package main.java.LinkedListdemo;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* DateTime: 2016/11/4 15:02
* 功能:
* 思路:
*/
public class MyLinkedList<E> implements Iterable<E> {
/*
嵌套类 节点类
*/
private static class Node<E>{
public E data;
public Node<E> prev;
public Node<E> next;
public Node(E e,Node<E> p,Node<E> q){
data=e; prev=p; next=q;
}
}
//类属性成员
private int size;
private int modCount=0;
private Node<E> beginMarker;
private Node<E> endMarker;
//构造函数
public MyLinkedList(){
doInit();
}
//清除函数
public void clear(){
doInit();
}
//新增函数
public void add(E value){
add(size(),value);
}
public void add(int index,E value){
addBefore(getNode(index,0,size()),value);
}
//清除函数
public void doInit(){
beginMarker=new Node<E>(null,null,null);
endMarker=new Node<E>(null,beginMarker,null);
beginMarker.next=endMarker;
size=0;
modCount++;
}
//链表元素个数
public int size(){
return this.size;
}
//判断是否为空
public boolean isEmpty(){
return 0==size();
}
//获得结点的数据
public E get(int index){
return getNode(index).data;
}
//设置结点数据并返回旧的值
public E set(int index,E value){
Node<E> p=getNode(index);
E oldValue=p.data;
p.data=value;
return oldValue;
}
//获得结点数据
private Node<E> getNode(int index){
return getNode(index,0,size()-1);
}
//获得结点数据
private Node<E> getNode(int index,int lower,int upper){
Node<E> p;
if(index<lower || index>upper){
throw new IndexOutOfBoundsException();
}
if(index<size()/2){
p=beginMarker.next;
for ( int i = 0; i < index; i++ ) {
p=p.next;
}
}else {
p=endMarker;
for ( int i = size(); i > index; i-- ) {
p=p.prev;
}
}
return p;
}
//在p结点之前添加节点
public void addBefore(Node<E> p,E value){
Node<E> node=new Node<E>(value,p.prev,p);
node.prev.next=node;
p.prev=node;
this.size++;
this.modCount++;
}
//移除结点的数据-根据下表
public E remove(int index){
return remove(getNode(index));
}
//移除结点的数据-根据结点
public E remove(Node<E> p){
p.next.prev=p.prev;
p.prev.next=p.next;
size--;
modCount++;
return p.data;
}
@Override
public Iterator<E> iterator() {
return new LinkedListIterator();
}
private class LinkedListIterator implements Iterator<E>{
private Node<E> current=beginMarker.next;//当前指针位置
private int expectedModCount=modCount;
private boolean okToRemove=false;
@Override
public boolean hasNext() {
return current!=endMarker;
}
@Override
public E next() {
if(modCount!=expectedModCount)
throw new ConcurrentModificationException();
if(!hasNext())
throw new NoSuchElementException();
E nextValue=current.data;
current=current.next;
okToRemove=true;
return nextValue;
}
@Override
public void remove() {
if(modCount!=expectedModCount)
throw new ConcurrentModificationException();
if(!okToRemove)
throw new IllegalStateException();
MyLinkedList.this.remove(current.prev);
expectedModCount++;
okToRemove=false;
}
}
}
测试:
package test.java;
import main.java.LinkedListdemo.MyLinkedList;
import java.util.Iterator;
/**
* DateTime: 2016/11/4 16:00
* 功能:
* 思路:
*/
public class MyLinkedListTest {
public static void main(String[] args) {
MyLinkedList linkedList=new MyLinkedList();
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);
linkedList.add(4);
Iterator iterator=linkedList.iterator();
while ( iterator.hasNext() ){
System.out.println(iterator.next());
}
}
}