java LinkedList 的实现

LinkedList 的实现

package com.meteor.algorithm;

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

/**
 * LinkedList的实现
 * Created by Meteor on 2016/3/21.
 */
public class MyLinkedList<T> implements Iterable<T> {

    private int theSize;
    private int modCount;
    private Node<T> beginMarker;
    private Node<T> endMarker;

    public MyLinkedList(){
        clear();
    }

    public int size(){
        return theSize;
    }

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

    public boolean add(T d){
        add(size(),d);
        return true;
    }

    public void add(int idx,T d){
        addBefore(getNode(idx),d);
    };

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

    public T set(int idx,T newData){
        Node<T> node = getNode(idx);
        T oldData = node.data;
        node.data = newData;
        return oldData;
    }

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

    private T remove(Node<T> node){
        node.prev.next = node.next;
        node.next.prev = node.prev;
        theSize--;
        modCount++;

        return node.data;
    }

    private void addBefore(Node<T> p,T d){
        Node<T> newNode = new Node<T>(d,p.prev,p);
        newNode.prev.next = newNode;
        p.prev = newNode;
        theSize++;
        modCount++;
    }

    private Node<T> getNode(int idx){
        Node<T> p;

        if (idx<0||idx>=size()){
            throw new IndexOutOfBoundsException();
        }

        if(idx<size()/2){
            p = beginMarker.next;
            for (int i = 0; i < idx; i++) {
                p = p.next;
            }
        }else {
            p = endMarker.prev;
            for (int i = size(); i >idx; i--) {
                p = p.prev;
            }
        }

        return p;
    }

    public void clear(){
        beginMarker = new Node<T>(null,null,null);
        endMarker = new Node<T>(null,beginMarker,null);
        beginMarker.next = endMarker;
        theSize = 0;
        modCount++;
    }

    private static class Node<T>{

        public T data;
        public Node<T> prev;
        public Node<T> next;
        public Node(T d,Node<T> p,Node<T> n){
            data = d;
            prev = p;
            next = n;
        }
    }

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

    private class LinkedListIterator implements Iterator<T>{

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


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

        public T next() {
            if (modCount!=expectedModCount){
                throw new ConcurrentModificationException();
            }

            if(!hasNext()){
                throw new NoSuchElementException();
            }

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

        public void remove(){
            if(modCount!=expectedModCount){
                throw new ConcurrentModificationException();
            }
            if (!okToRemove){
                throw new IllegalStateException();
            }

            MyLinkedList.this.remove(current.prev);
            okToRemove = false;
            expectedModCount++;
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值