用链表实现LinkedList


public class myLinkedList implements Iterable {
    private int thesize;    //存储当前链表的长度
    private int modcount = 0;
    private Node beginMarker;   //链表的头节点,其next指向链表的第一个节点
    private Node endMarker;     //链表的尾节点,其prev指向链表的最后一个节点

    public myLinkedList() {
        clear();
    }

    public void clear() {
        beginMarker = new Node(null, null, endMarker);
        endMarker = new Node(null, beginMarker, null);

        thesize = 0;
        modcount++;
    }

    public int size() {
        return thesize;
    }

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

    public boolean add(Object x) {
        add(thesize, x);
        return true;
    }

    public void add(int idx, Object x) {
        addbefore(getNode(idx), x);
    }

    public Object set(int index, Object newval) {
        Node p = getNode(index);
        Object oldval = p.date;
        p.date = newval;
        return oldval;

    }

    public Object remove(int index) {
        return remove(getNode(index));
    }

    private Object remove(Node p) {
        p.prev.next = p.next;
        p.next.prev = p.prev;
        thesize--;
        return p.date;
    }


    private Node getNode(int index) {
        if (index < 0 || index > size()) {
            throw new IndexOutOfBoundsException();
        }
        Node p;
        if (index < size() / 2) {
            p = beginMarker;
            for (int i = 0; i < index; i++) {
                p = p.next;
            }
            return p;
        } else {
            p = endMarker;
            for (int i = size(); i > index; i--) {
                p = p.prev;
            }
            return p;
        }
    }

    private void addbefore(Node p, Object x) {
        Node newnode = new Node(x, p.prev, p);
        newnode.prev.next = newnode;
        p.prev = newnode;
        thesize++;
        modcount++;
    }

    @Override
    public Iterator iterator() {
        return new LinkedListIterator();
    }



    private static class Node {
        public Object date;
        public Node prev;
        public Node next;

        public Node(Object d, Node p, Node n) {
            this.date = d;
            this.prev = p;
            this.next = n;
        }
    }


    public class LinkedListIterator implements Iterator {

        private Node current = beginMarker.next;
        private int expectedModCount = modcount;
        private boolean oktoremove = false;

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

        @Override
        public Object next() {
            if (modcount != expectedModCount) {
                throw new ConcurrentModificationException();
            }
            if (!hasNext()) {
                try {
                    throw new NoSuchFieldException();
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                }
            }
            Object nextitem = current.date;
            current = current.next;
            oktoremove = true;
            return nextitem;

        }

        @Override
        public void remove() {
            if (modcount != expectedModCount) {
                throw new ConcurrentModificationException();
            }
            if (!hasNext()) {
                try {
                    throw new NoSuchFieldException();
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                }
            }
//            myLinkedList.this.remove(current.prev);
            new myLinkedList().remove(current.prev);
            oktoremove = false;
            expectedModCount++;
        }

        @Override
        public void forEachRemaining(Consumer action) {

        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值