栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 其他

用链表实现LinkedList

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用链表实现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) {

        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/279561.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号