java数据结构——自定义实现一个简单的LinkedList

LinkedList由一个双向链表实现
LinkedList中存储的是数据节点,所以声明一个内部节点数据类型
这个数据节点结构中主要应该包括三个部分:
数据域(data):存放数据
next域:存放下一个节点的应用
prev域:存放上一个节点的引用
//Linked由一个双向链表实现
public class MyLinkedList<T> implements Iterable<T> {
    /*LinkedList中存储的是数据节点,所以声明一个内部节点数据类型
    这个数据节点结构中主要应该包括三个部分:
    数据域(data):存放数据
    next域:存放下一个节点的应用
    prev域:存放上一个节点的引用
    */
    public  static class Node<T>{
        public Node(T d,Node<T> p,Node<T> n){
            data=d;         //数据
            prev=p;         //前驱
            next=n;         //后继
        }
        public T data;
        public Node<T> prev;
        public Node<T> next;


    }

    private int theSize;   //容器中元素的个数
    private  int modCount;
    private Node<T> beginMaker;                 //头节点
    private Node<T> endMaker;                   //尾节点
    //初始化容器
    public MyLinkedList(){
        doClear();
    }

    //清理容器
    public void clear(){
        doClear();
    }

    private void doClear(){
        beginMaker=new Node <T>(null,null,null);    //初始化头节点
        endMaker=new Node <T>(null,beginMaker,null);    //初始化尾节点,前驱指向头节点
        beginMaker.next=endMaker;                       //头节点的后继指向尾节点
        theSize = 0;   //元素个数初始化为0
        //modCount++;
    }

    //获取容器元素个数
    public int size(){
    return theSize;
    }

    //判断容器是否为空
    public boolean idEmpty(){
        return size()==0;
    }

    //添加元素
    public boolean add(T x){
        add(size(),x);
        return true;
    }

    //指定位置添加元素
    public void add(int idx,T x){
        addBefore(getNode(idx,0,size()),x);
    }

    //获得指定索引位置的元素
    public T get(int idx){
        return getNode(idx).data;
    }

    //替换指定索引位置元素的值,并返回被替换的旧元素
    public T set(int idx,T newval){
        Node<T> p=getNode(idx);
        T old=p.data;
        p.data=newval;
        return old;
    }

    //删除指定位置的元素
    public T remove(int idx){
    return remove(getNode(idx));
    }

    //指定节点之前插入值
    public void addBefore(Node<T> p,T x){
        Node<T> newnode=new Node <>(x,p.prev,p);
        newnode.prev.next=newnode;
        p.prev=newnode;
        theSize++;
        //modCount++;

    }
    //移除指定节点
    private  T remove(Node<T> p){
        p.next.prev=p.prev;
        p.prev.next=p.next;
        theSize--;
        modCount++;
        return p.data;
    }

    public Node<T> getNode(int idx){
        return getNode(idx,0,size()-1);
    }


    //在指定范围(lower-upper)查找指定索引(idx)的元素
    private Node<T> getNode(int idx,int lower,int upper){
        Node<T> p;
        if (idx<lower || idx>upper)
            throw  new IndexOutOfBoundsException();

        if (idx<size()/2){
            p=beginMaker.next;
            for(int i=0;i<idx;i++){
                p=p.next;

            }
        }else{
            p=endMaker;
            for(int i=size();i>idx;i--){
                p=p.prev;
            }
        }
        return p;

    }

    public Iterator<T> it(){
        return new LinkedListIterator();
    }

    public class LinkedListIterator implements Iterator<T>{

        private Node<T>current=beginMaker.next;
        private int expectedModCount = modCount;
        private boolean okToRemove = false;


        @Override
        public boolean hasNext() {
            return current!=endMaker;
        }

        @Override
        public T next() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
            if(!hasNext())
                throw new NoSuchElementException();

            T nextItem = current.data;
            current = current.next;
            okToRemove = true;
            return nextItem;
        }

        @Override
        public void remove() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
            if (!okToRemove)
                throw new IllegalStateException();
            MyLinkedList.this.remove(current.prev);
            expectedModCount++;
            okToRemove =false;

        }
    }
    @Override
    public Iterator<T> iterator() {
        return null;
    }

}

猜你喜欢

转载自blog.csdn.net/qq_40692753/article/details/82984459