java.util.LinkedList

说明:Linked list implementation of the List interface. Implements all optional list operations, and permits all elements (includingnull). In addition to implementing the List interface, the LinkedList class provides uniformly named methods to get, remove andinsert an element at the beginning and end of the list. These operations allow linked lists to be used as a stack,queue, or double-ended queue

The class implements the Deque interface, providing first-in-first-out queue operations foradd, poll, along with other stack and deque operations


LinkedList类内部维护一个Entry类,private static class Entry<E> {
E element;
Entry<E> next;
Entry<E> previous;


Entry(E element, Entry<E> next, Entry<E> previous) {
   this.element = element;
   this.next = next;
   this.previous = previous;
}

LinkedList类是通过双向链表实现的,public LinkedList() {
        header.next = header.previous = header;
    }


方法:public E getFirst(),public E getLast(),public E removeFirst(),public E removeLast(),public void addFirst(E e) ,

public void addLast(E e) ,public boolean contains(Object o),


public boolean add(E e) {
addBefore(e, header);
        return true;
    }

public boolean remove(Object o) {
        if (o==null) {
            for (Entry<E> e = header.next; e != header; e = e.next) {
                if (e.element==null) {
                    remove(e);
                    return true;
                }
            }
        } else {
            for (Entry<E> e = header.next; e != header; e = e.next) {
                if (o.equals(e.element)) {
                    remove(e);
                    return true;
                }
            }
        }
        return false;
    }

实际调用其重载函数remove(Entry<e> e)

public Object[] toArray() {
Object[] result = new Object[size];
        int i = 0;
        for (Entry<E> e = header.next; e != header; e = e.next)
            result[i++] = e.element;
return result;
    }



public boolean addAll(int index, Collection<? extends E> c) {
        if (index < 0 || index > size)
            throw new IndexOutOfBoundsException("Index: "+index+
                                                ", Size: "+size);
        Object[] a = c.toArray();
        int numNew = a.length;
        if (numNew==0)
            return false;
modCount++;


        Entry<E> successor = (index==size ? header : entry(index));
        Entry<E> predecessor = successor.previous;
for (int i=0; i<numNew; i++) {
            Entry<E> e = new Entry<E>((E)a[i], successor, predecessor);
            predecessor.next = e;
            predecessor = e;
        }

函数传递了一个index并通过Entry(index)获得当前链表的index个节点以及前一个节点,然后在此将Collection所有元素加入。且,Entry(index)多次被其他函数调用。

public void clear() {
        Entry<E> e = header.next;
        while (e != header) {
            Entry<E> next = e.next;
            e.next = e.previous = null;
            e.element = null;
            e = next;
        }
        header.next = header.previous = header;
        size = 0;
modCount++;
    }

全部置空,但并没有delete掉


public E get(int index) {
        return entry(index).element;
    }


public E set(int index, E element) {
        Entry<E> e = entry(index);
        E oldVal = e.element;
        e.element = element;
        return oldVal;
    }

public void add(int index, E element) {
        addBefore(element, (index==size ? header : entry(index)));
    }

public E remove(int index) {
        return remove(entry(index));
    }

下面是Entry(index)具体实现

 private Entry<E> entry(int index) {
        if (index < 0 || index >= size)
            throw new IndexOutOfBoundsException("Index: "+index+
                                                ", Size: "+size);
        Entry<E> e = header;
        if (index < (size >> 1)) {
            for (int i = 0; i <= index; i++)
                e = e.next;
        } else {
            for (int i = size; i > index; i--)
                e = e.previous;
        }
        return e;
    }


  public E peek() {
        if (size==0)
            return null;
        return getFirst();
    } 


public E poll() {
        if (size==0)
            return null;
        return removeFirst();
    }


 public E remove() {
        return removeFirst();

}                      和poll()具体实现是一样

差不多就这样了。其他也没什么图特别的了.



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值