相比较myarraylist只是实现原理,myLinkedList的鲁棒性好。只是实现简单功能,部分功能没有实现。
public class MyLinkedList<Anytype> implements Iterable<Anytype> {
/**
* 成员变量:长度、操作次数、头节点、尾节点
* 增删改查 都是先找到节点 在节点上操作 查询操作:时间复杂度 0(n/2);
*
*/
private int theSize;
private int modCount;
private Node<Anytype> beginNode;
private Node<Anytype> endNode;
public MyLinkedList(){
doInClear();
}
public void clear(){
doInClear();
}
public boolean isEmpty(){
return theSize == 0;
}
public int size(){
return theSize;
}
public Anytype get(){
return get(theSize - 1);
}
public Anytype get(int index){
return getNode(index).item;
}
public Anytype set(int index,Anytype item){
Anytype p = null;
Node<Anytype> node = getNode(index);
p = node.item;
node.item = item;
return p;
}
//直接删除最后一个
public Anytype remove(){
Node<Anytype> node = endNode.pre;
return remove(node);
}
private Anytype remove(Node<Anytype> node){
node.next.pre = node.pre;
node.pre.next = node.next;
theSize --;
modCount ++;
return node.item;
}
public Anytype remove(int index){
//getNode 会检测参数
Node<Anytype> node = getNode(index);
return remove(node);
}
public void add(Anytype item){
//直接加在最后就完事了
addBefore(endNode,item);
modCount ++;
theSize ++;
}
public void add(int index,Anytype item){
//getNode 有index的判断。item 是否可为null 按要求而定。
// if(item == null){
//
// }
addBefore(getNode(index),item);
}
private void addBefore(Node<Anytype> node, Anytype item){
//从右向左赋值 最后修改node.pre的值。
node.pre = node.pre.next = new Node<Anytype>(item,node.pre,node);
modCount ++;
theSize ++;
}
private Node<Anytype> getNode(int index){
if(index <0 || index>theSize-1){
throw new IndexOutOfBoundsException();
}
Node<Anytype> p = null ;
if(index < theSize/2){
//靠近beginNode
for (int i = theSize;i>index;i--){
p = endNode.pre;
}
}else{
p = beginNode.next;
for (int i = 0;i<index;i++){
p = beginNode.next;
}
//靠近endBode
}
return p;
}
private void doInClear(){
theSize = 0;
modCount ++;
beginNode = new Node<Anytype>(null,null,null);
endNode = new Node<Anytype>(null,beginNode,null);
beginNode.next = endNode;
}
@NonNull
@Override
public Iterator<Anytype> iterator() {
return null;
}
@Override
public void forEach(Consumer<? super Anytype> action) {
}
@Override
public Spliterator<Anytype> spliterator() {
return null;
}
static private class Node<Anytype>{
//链表节点。
/**
* 有三个参数 item值、previouse节点、next节点
*/
public Anytype item;
public Node<Anytype> pre;
public Node<Anytype> next;
public Node(Anytype item,Node pre,Node next){
this.item = item;
this.pre = pre;
this.next = next;
}
}
private class MyIterator implements Iterator<Anytype>{
private int exceptedModCount;
private Node<Anytype> current = beginNode.next;
private boolean canToNext;
public MyIterator(){
exceptedModCount = modCount;
canToNext = false;
}
@Override
public boolean hasNext() {
if(exceptedModCount != modCount){
throw new ConcurrentModificationException();
}
return current.next != null;
}
@Override
public Anytype next() {
if(exceptedModCount != modCount){
throw new ConcurrentModificationException();
}
if(!hasNext()){
throw new NoSuchElementException();
}
current = current.next;
canToNext = true;
return current.item;
}
@Override
public void remove() {
if(exceptedModCount != modCount){
throw new ConcurrentModificationException();
}
if(!canToNext){
throw new IllegalStateException();
}
MyLinkedList.this.remove(current.pre);
canToNext = false;
exceptedModCount ++;
}
}
}