LinkedList的编写 --MyLinkedList

LinkedList的编写 --MyLinkedList

package ADT;

import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class MyLinkedList<AnyType> implements Iterable<AnyType>{
    private int theSize;//元素的个数
    private int modCount = 0;//记录自从构造以来对链表所做的改变的次数,每次对add或remove的调用都将更新modCount
    private Node<AnyType> beginMarker;//头结点
    private Node<AnyType> endMarker;//尾结点

    public MyLinkedList(){
        doClear();
    }

    private static class Node<AnyType>{//嵌套类Node
        public AnyType data;
        public Node<AnyType> prev;//前驱结点
        public Node<AnyType> next;//后继结点

        public Node(AnyType data, Node<AnyType> prev, Node<AnyType> next) {
            this.data = data;
            this.prev = prev;
            this.next = next;
        }
    }

    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 add(AnyType x){//添加元素
        add(theSize,x);
        return true;
    }

    public void add(int idx,AnyType x){
        addBefore(getNode(idx,0,theSize),x);
    }

    public AnyType get(int idx){
        return getNode(idx).data;
    }

    public AnyType set(int idx,AnyType newValue){
        Node<AnyType> p = getNode(idx);
        AnyType oldValue = p.data;
        p.data = newValue;
        return oldValue;
    }

    private  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++;
    }

    public AnyType remove(int idx){
        return remove(getNode(idx));
    }

    private AnyType remove(Node<AnyType> p){
        p.next.prev=p.prev;
        p.prev.next=p.next;
        theSize--;
        modCount--;
        return p.data;
    }

    private Node<AnyType> getNode(int idx){
        return getNode(idx, 0, theSize-1);
    }

    private Node<AnyType> getNode(int idx, int lower, int upper){
        Node<AnyType> p;
        if(idx < lower || idx > upper)
            throw new ArrayIndexOutOfBoundsException();
        if(idx < theSize / 2){
            p = beginMarker.next;
            for(int i = 0; i < idx; i++){
                p = p.next;
            }
        }else {
            p = endMarker;
            for(int i = theSize; i > idx; i--){
                p = p.prev;
            }
        }
        return p;
    }

    @Override
    public Iterator<AnyType> iterator() {
        return new MyLinkedListIterator();
    }
    private class MyLinkedListIterator implements Iterator<AnyType>{
        private Node<AnyType> current = beginMarker.next;
        private int expectedModCount = modCount;//初始化迭代器时expectedModCount记录modCount,用于判断在迭代器遍历时是否更改了集合的结构。当调用迭代器的remove方法时,modCount和expectedModCount都会--;在迭代器遍历时,如果改变集合结构(即调用集合的remove方法时),迭代器中的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;
        }

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

}

LinkedList采用双链表来实现,而且保留了头结点和尾结点,这样做可以保持每个操作花费常数时间的代价。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值