java实现Linkedlist

package myDataStructures;
import myDataStructures.MyArrayList;

import javax.swing.text.html.HTMLDocument;
import java.util.*;

public class MyLinkedList<T> {

    private int theSize;
    private int modCount; //统计对双向链表的修改的次数
    private Node<T> theBeginNode;
    private Node<T> theEndNode;
    private Node<T> theMiddleNode;
    private int indexOfMiddle;

    public MyLinkedList(){

        doClear();
    }

    public boolean clear(){
        return doClear();
    }

    private boolean doClear(){

        this.theSize=0;
        this.theBeginNode=new Node<>(null,this.theEndNode,null); 
        this.theEndNode=new Node<>(null,null,this.theBeginNode); 
        this.theMiddleNode=this.theBeginNode;
        this.indexOfMiddle=(int)theSize/2;
        this.modCount=0;
        return true;
    }

    public void add(T value){ 
        add(theSize, value);
    }

    public void add(int index,T value){ 
        if ((int)theSize/2 > indexOfMiddle){    
            theMiddleNode=returnThePredNode(0,theSize,(int)theSize/2);        
            indexOfMiddle=(int)theSize/2;
        }
        Node<T> predNodeOfIndexWantToInsert= returnThePredNode(0, theSize, index);
        Node<T> indexData=new Node<>(value,predNodeOfIndexWantToInsert.next,predNodeOfIndexWantToInsert);
        predNodeOfIndexWantToInsert.next=indexData;
    }

    public T get(){
        return get(theSize-1);
    }

    public boolean isEmpty(){
        return theSize==0;
    }

    public T get(int index){
        Node<T> nodeGet= returnThePredNode(0,theSize-1,index);
        
        return nodeGet.data;
    }

    public returnTwo<T,Boolean> remove(){
        return remove(theSize-1);
    }

    public returnTwo<T,Boolean> remove(int index){
        Node<T> predOfWantRemove= returnThePredNode(0,theSize-1,index);
        T oldData=predOfWantRemove.next.data;
        predOfWantRemove.next=predOfWantRemove.next.next;
        predOfWantRemove.next.pred=predOfWantRemove; //删除了该节点的前后向next链
        return new returnTwo<>(oldData,true);
    }

    public MyLinkedListIterator<T> iterator(){
        return new MyLinkedListIterator<>(theBeginNode);
    }

    public Node<T> returnThePredNode(int lowBaseLine, int highBaseLine, int index){
        Node<T> rePred; 
        if (index< lowBaseLine || index> highBaseLine){
            throw new ArrayIndexOutOfBoundsException(index);
        }
        
        int flag=(int)theSize/2;
        if (index == flag && indexOfMiddle == flag){
//            ++modCount;
            return theMiddleNode;
        }
        if (index< flag){
            int newFlag=(int)(indexOfMiddle+theSize)/2;
            if (index> newFlag){
                rePred= theEndNode.pred; 
                for (int i=theSize-1; i>= index; i--){ 
                    rePred= rePred.pred;
                }
//                ++modCount;
                return rePred;
            }
            else {
                rePred= theMiddleNode.pred;
                for (int i=indexOfMiddle-1; i< index-1; i++){
                    rePred=rePred.next;
                }
                return rePred;
            }
        }
        else {
            int newFlag=(int)(indexOfMiddle)/2;
            if (index> newFlag){
                rePred= theMiddleNode.pred; 
                for (int i=indexOfMiddle-1; i>= index; i--){ 
                    rePred= rePred.pred;
                }
//                ++modCount;
                return rePred;
            }
            else {
                rePred= theBeginNode.next;
                for (int i=0; i< index-1; i++){
                    rePred=rePred.next;
                }
                return rePred;
            }
        }
    }

    public int size(){
        return theSize; 
    }

    // 实现节点 已经前向后向的next链
    private static class Node<T>{
        private Node<T> next;
        private Node<T> pred;
        private T data;

        private Node(T d, Node<T> n, Node<T> p){
            this.next=n;
            this.pred=p;
            this.data=d;
        }


    }

    private class returnTwo<A,B>{
        A a;
        B b;
        private returnTwo(A a,B b){
            this.a=a;
            this.b=b;
        }
    }

    private class MyLinkedListIterator<T> implements Iterator<T>{ //实现了foreach

        private Node<T> currentNode;
        private int nowIndex;

        private MyLinkedListIterator(Node<T> a){
            currentNode= a;
            nowIndex= -1;
        }

        @Override
        public T next() {
            if (! hasNext()){
                throw new ArrayIndexOutOfBoundsException(nowIndex+1);
            }
            T returnData=currentNode.next.data;
            currentNode=currentNode.next; 
            ++nowIndex;
            return returnData;
        }

        @Override
        public boolean hasNext() {
            return (currentNode.next != null);
        }

        @Override
        public void remove(){
            MyLinkedList.this.remove(nowIndex--);
        }
    }



}

class Test{
    public static void main(String[] args){
        MyLinkedList<Integer> a=new MyLinkedList<>();
        System.out.println(a.toString());
        MyLinkedList<Integer> b=a; 
//        System.out.println(a.theSize);
//        b.theSize=10;
//        System.out.println(a.theSize);
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值