LinkedList源码
来源:互联网 发布:立讯精密 知乎 编辑:程序博客网 时间:2024/06/06 13:59
LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable
1.类变量&常量
transient int size = 0; //list的大小
transient Node<E> first; //list的首部
transient Node<E> last; //list的尾部
2.构造函数
public LinkedList() { //空的list }
public LinkedList(Collection<? extends E> c) { //将一个容器c加入到list中 this(); addAll(c); }
3.内部类
ListItr implements ListIterator<E> : 对list进行遍历,可以提供向前遍历
DescendingIterator implements Iterator<E> : 以倒序形式对list进行输出
static class Node<E> : list中的元素,以Node形式保存,含有 E item; Node<E> next; Node<E> prev; Node(Node<E> prev, E element, Node<E> next) { this.item = element; this.next = next; this.prev = prev; }
4.重要函数
1.链表插入函数 //头部插入法 private void linkFirst(E e) { final Node<E> f = first; final Node<E> newNode = new Node<>(null, e, f); first = newNode; if (f == null) //如果list之前为空,则让last=newNode last = newNode; else f.prev = newNode; size++; //容量+1 modCount++; } //尾部插入法 void linkLast(E e) { final Node<E> l = last; final Node<E> newNode = new Node<>(l, e, null); last = newNode; if (l == null) first = newNode; else l.next = newNode; size++; modCount++; } //插入到某个节点之前 void linkBefore(E e, Node<E> succ) { // assert succ != null; final Node<E> pred = succ.prev; final Node<E> newNode = new Node<>(pred, e, succ); succ.prev = newNode; if (pred == null) //若为空,则为头部插入法 first = newNode; else pred.next = newNode; size++; modCount++; }
2.返回指定位置的元素,使用二分法查找 Node<E> node(int index) { if (index < (size >> 1)) { //从first开始找 Node<E> x = first; for (int i = 0; i < index; i++) x = x.next; return x; } else { //从last开始找 Node<E> x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; } }
3.add函数,添加元素 public boolean add(E e) { linkLast(e); //使用尾部插入 return true; } public void add(int index, E element) { checkPositionIndex(index); //查看index是否越界 if (index == size) linkLast(element); //直接在list的最后添加元素 else linkBefore(element, node(index)); //在node(index)的前面添加元素 } public void addLast(E e) { linkLast(e); } public void addFirst(E e) { linkFirst(e); } public boolean addAll(Collection<? extends E> c) { //插入某个容器类 return addAll(size, c); } public boolean addAll(int index, Collection<? extends E> c) { checkPositionIndex(index); //检查边界条件 Object[] a = c.toArray(); int numNew = a.length; if (numNew == 0) //容器为空 return false; Node<E> pred, succ; if (index == size) { //插入到尾部 succ = null; pred = last; } else { succ = node(index); //插入到node(index)的前面 pred = succ.prev; } for (Object o : a) { E e = (E) o; Node<E> newNode = new Node<>(pred, e, null); if (pred == null) //从头部插入,让容器的第一个元素作为first,然后这一段if代码不会再执行 first = newNode; else pred.next = newNode; pred = newNode; } if (succ == null) { //尾部插入,另last=容器的最后一个元素 last = pred; } else { //非尾部插入 pred.next = succ; succ.prev = pred; } size += numNew; //size = size + 容器中元素个数 modCount++; return true; }
4.返回指定元素的位置 public int indexOf(Object o) { int index = 0; if (o == null) { for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) return index; index++; } } else { for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) return index; index++; } } return -1; } //从尾部开始查找 public int lastIndexOf(Object o) { int index = size; if (o == null) { for (Node<E> x = last; x != null; x = x.prev) { index--; if (x.item == null) return index; } } else { for (Node<E> x = last; x != null; x = x.prev) { index--; if (o.equals(x.item)) return index; } } return -1; }
5.返回ArrayList中元素的个数 public int size() { return size; }
6.变成Array数组类型,同ArrayList类似 public Object[] toArray() { Object[] result = new Object[size]; int i = 0; for (Node<E> x = first; x != null; x = x.next) result[i++] = x.item; return result; } public <T> T[] toArray(T[] a) { if (a.length < size) a = (T[])java.lang.reflect.Array.newInstance( a.getClass().getComponentType(), size); int i = 0; Object[] result = a; for (Node<E> x = first; x != null; x = x.next) result[i++] = x.item; if (a.length > size) a[size] = null; return a; }
7.克隆函数,类似ArrayList的clone()函数 public Object clone() { LinkedList<E> clone = superClone(); // 重置新的list的状态位 clone.first = clone.last = null; clone.size = 0; clone.modCount = 0; // Initialize clone with our elements for (Node<E> x = first; x != null; x = x.next) clone.add(x.item); return clone; } private LinkedList<E> superClone() { try { return (LinkedList<E>) super.clone(); } catch (CloneNotSupportedException e) { throw new InternalError(e); } }
8.内部辅助unlink函数,删除某个节点 //删除list的头结点,返回头结点的值 private E unlinkFirst(Node<E> f) { final E element = f.item; final Node<E> next = f.next; f.item = null; f.next = null; // help GC first = next; if (next == null) last = null; else next.prev = null; size--; modCount++; return element; } //删除尾结点,返回尾结点的值 private E unlinkLast(Node<E> l) { final E element = l.item; final Node<E> prev = l.prev; l.item = null; l.prev = null; // help GC last = prev; if (prev == null) first = null; else prev.next = null; size--; modCount++; return element; } //将x结点删除 E unlink(Node<E> x) { final E element = x.item; final Node<E> next = x.next; final Node<E> prev = x.prev; if (prev == null) { first = next; } else { prev.next = next; x.prev = null; } if (next == null) { last = prev; } else { next.prev = prev; x.next = null; } x.item = null; size--; modCount++; return element; }
9.删除函数remove,public类型 /* 无参数的remove函数,调用removeFirst()函数 */ public E remove() { return removeFirst(); } //删除第一个元素 public E removeFirst() { final Node<E> f = first; if (f == null) throw new NoSuchElementException(); return unlinkFirst(f); } //删除最后一个元素 public E removeLast() { final Node<E> l = last; if (l == null) throw new NoSuchElementException(); return unlinkLast(l); } //删除指定位置的结点 public E remove(int index) { checkElementIndex(index); //检查边界条件 return unlink(node(index)); } //删除与指定元素值相同的所有结点 public boolean remove(Object o) { if (o == null) { for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) { //x.item == null为判断条件,而非x==null,因为若如此则无法调用unlink函数 //因为unlink函数中null没有pred和succ unlink(x); return true; } } } else { for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) { unlink(x); return true; } } } return false; } //删除list中第一次出现元素o的位置结点 public boolean removeFirstOccurrence(Object o) { return remove(o); } //删除list中最后一次出现该元素的位置的结点 public boolean removeLastOccurrence(Object o) { if (o == null) { for (Node<E> x = last; x != null; x = x.prev) { //从last开始遍历则可找到 if (x.item == null) { unlink(x); return true; } } } else { for (Node<E> x = last; x != null; x = x.prev) { if (o.equals(x.item)) { unlink(x); return true; } } } return false; }
10.类似于其他数据结构的函数 public boolean offer(E e) { //添加到队列末尾,返回boolean类型 return add(e); } public boolean offerFirst(E e) { //添加到栈中,返回Boolean类型 addFirst(e); return true; } public void push(E e) { //添加到第一个元素,类似栈 addFirst(e); } public E peek() { //取list的第一个元素 final Node<E> f = first; return (f == null) ? null : f.item; } public E peekFirst() { final Node<E> f = first; return (f == null) ? null : f.item; } public E peekLast() { final Node<E> l = last; return (l == null) ? null : l.item; } public E poll() { //弹出第一个元素,若为空则返回null final Node<E> f = first; return (f == null) ? null : unlinkFirst(f); } public E pollFirst() { final Node<E> f = first; return (f == null) ? null : unlinkFirst(f); } public E pollLast() { final Node<E> l = last; return (l == null) ? null : unlinkLast(l); } public E pop() { //弹出第一个元素,若为空则报错NoSuchElementException return removeFirst(); }
11.clear函数,清除list public void clear() { //为帮助gc,将list中的所有node全部设置为null for (Node<E> x = first; x != null; ) { Node<E> next = x.next; x.item = null; x.next = null; x.prev = null; x = next; } first = last = null; size = 0; modCount++; }
0 0
- LinkedList源码
- LinkedList源码
- LinkedList源码
- LinkedList源码
- 【源码】LinkedList源码剖析
- JDK源码-LinkedList源码
- LinkedList 源码分析
- LinkedList源码分析
- ArrayList LinkedList 源码分析
- LinkedList源码浅析
- linkedList 源码解析
- ArrayList,LinkedList源码分析
- 源码分析之LinkedList
- Java源码之LinkedList
- LinkedList源码解析
- LinkedList源码解析
- LinkedList源码分析
- LinkedList源码分析
- Android - android.view.InflateException: Binary XML file line #: Error inflating class fragment
- HashSet源码
- JDBC结合MySql完成增删改查
- 在PPT中FLASH的使用
- linux 下文件夹的复制、覆盖以及确认问题解决
- LinkedList源码
- 非常好的理解遗传算法的例子
- ArrayList源码
- Git客户端图文详解如何安装配置GitHub操作流程攻略
- sd acm省赛(c-e
- 切图的那点事儿
- Mac配置hosts文件来访问局限网站
- 自定义进度条
- bzoj 1037 生日聚会 party