当前位置:   article > 正文

深入理解LinkedList_linkedlist 左右哪边是头

linkedlist 左右哪边是头

什么是LinkedList

概念

LinkedList是基于链表实现的,所以先讲解一下什么是链表。链表是一种线性的存储结构,意思是将要存储的数据存在一个存储单元里面,这个存储单元里面除了存放有待存储的数据以外,还存储有其下一个存储单元的地址(下一个存储单元的地址是必要的,有些存储结构还存放有其前一个存储单元的地址),每次查找数据的时候,通过某个存储单元中的下一个存储单元的地址寻找其后面的那个存储单元。
理解:
LinkedList是一个双向链表,也就是说list中的每个元素,在存储自身值之外,还 额外存储了其前一个和后一个元素的地址,所以 也就可以很方便地根据当前元素获取到其前后的元素链表的尾部元素的后一个节点是链表的头节点;而链表的头结点前一个节点则是则是链表的尾节点(是不是有点像贪吃蛇最后 头吃到自己尾巴的样子,脑补下)既然是一个双向链表,那么必然有一个基本的存储单元。
相比于ArrayList,LinkedList他有不同之处,ArrayList直接继承AbstractList,而LinkedList继承自AbstractSequentialList,然后再继承自AbstractList.另外,LinkedList还实现了Dequeue接口,双端队列。

  1. public class LinkedList<E>
  2. extends AbstractSequentialList<E>
  3. implements List<E>, Deque<E>, Cloneable, java.io.Serializable{}

类的结构示意图

 一个结点

内部结构 

 

 从上图可以看出,LinkedList内部是一个双端链表结构,有两个变量,first指向链表头部,last指向链表尾部。

源码分析

LinkedList内部成员变量

  1. //链表大小
  2. transient int size = 0;
  3. /**
  4. * 指向第一个结点
  5. * Invariant: (first == null && last == null) ||
  6. * (first.prev == null && first.item != null)
  7. */
  8. transient Node<E> first;
  9. /**
  10. * 指向最后一个结点
  11. * Invariant: (first == null && last == null) ||
  12. * (last.next == null && last.item != null)
  13. */
  14. transient Node<E> last;

 Node类,为静态内部类

  1. private static class Node<E> {
  2. E item; //数据源
  3. Node<E> next; //后一个结点的地址
  4. Node<E> prev; //前一个结点的地址
  5. Node(Node<E> prev, E element, Node<E> next) {
  6. this.item = element;
  7. this.next = next;
  8. this.prev = prev;
  9. }
  10. }

构造方法

LinkedList有两个构造方法,一个用于构造一个空的链表,一个用已有的集合创建链表。

  1. public LinkedList() {
  2. }
  3. public LinkedList(Collection<? extends E> c) {
  4. this(); //此方法为无参构造
  5. addAll(c); //将集合c添加到链表中
  6. }
  7. public boolean addAll(Collection<? extends E> c) {
  8. return addAll(size, c); //调用addAll方法,含有两个参数,size和c
  9. }

List接口的添加操作

add(E e)

add(E e)用于将元素添加到链表尾部

  1. public boolean add(E e) {
  2. linkLast(e);
  3. return true;
  4. }
  5. void linkLast(E e) {
  6. final Node<E> l = last; //获取尾部元素
  7. final Node<E> newNode = new Node<>(l, e, null); //以尾部为前驱结点创建一个新结点
  8. last = newNode; //更新尾部结点为新结点
  9. if (l == null) //如果链表为空,那么该结点就是头结点也是尾结点
  10. first = newNode;
  11. else //不为空的话,那么该结点为尾部结点的后继结点
  12. l.next = newNode;
  13. size++; //长度自增
  14. modCount++; //操作次数自增
  15. }

LinkLast其实是一个尾插法,但是需要注意链表为空时头结点的处理。

add(int index,E e)

该方法用于在指定位置添加元素

  1. public void add(int index, E element) {
  2. checkPositionIndex(index);//检查索引是否越界
  3. if (index == size)//添加在链表尾部
  4. linkLast(element);
  5. else //添加在链表中间
  6. linkBefore(element, node(index));
  7. }

node(int index)方法

  1. Node<E> node(int index) {
  2. // assert isElementIndex(index);
  3. //如果索引位置靠链表前半部分,从头开始遍历
  4. if (index < (size >> 1)) {
  5. Node<E> x = first; //赋值给头结点
  6. for (int i = 0; i < index; i++)
  7. x = x.next;
  8. return x;
  9. } else {
  10. //否则从尾部开始遍历
  11. Node<E> x = last; //赋值给尾结点
  12. for (int i = size - 1; i > index; i--)
  13. x = x.prev;
  14. return x;
  15. }
  16. }

从上面看出node(int index)方法将根据index是靠近头部还是尾部选择不同的遍历方向

linkBefore()方法

  1. void linkBefore(E e, Node<E> succ) {
  2. // assert succ != null;
  3. final Node<E> pred = succ.prev;//找到succ结点的前驱
  4. final Node<E> newNode = new Node<>(pred, e, succ);//创建新的结点
  5. succ.prev = newNode;//succ结点的前驱指向新节点
  6. if (pred == null)
  7. first = newNode; //判断前驱是否为null,是的话newNode就是头结点
  8. else //不是的话前驱结点的next指向新节点
  9. pred.next = newNode;
  10. size++; //长度自增
  11. modCount++;
  12. }

 linkBefore()方法在第二个参数结点之前插入一个新结点

addAll()方法 

addAll有两个重装方法,一个参数的方法表示将集合元素添加到链表尾部,而两个参数的方法指定了开始插入的位置。

  1. //将集合插入链表尾部,即开始索引位置为size
  2. public boolean addAll(Collection<? extends E> c) {
  3. return addAll(size, c);
  4. }
  5. public boolean addAll(int index, Collection<? extends E> c) {
  6. checkPositionIndex(index); //检查index是否越界
  7. Object[] a = c.toArray(); //集合c转换为数组
  8. int numNew = a.length; //计算数组长度
  9. if (numNew == 0) //判断数组长度是否为0
  10. return false;
  11. Node<E> pred, succ; //定义前驱结点和后继结点
  12. if (index == size) { //如果index是末尾的话
  13. succ = null; //后继结点为null
  14. pred = last; //前驱结点作为尾结点
  15. } else {
  16. succ = node(index); //用node方法得到后继结点
  17. pred = succ.prev; //得到前驱结点
  18. }
  19. //遍历数据
  20. for (Object o : a) {
  21. @SuppressWarnings("unchecked") E e = (E) o; //强转为E类型
  22. //创建新的结点
  23. Node<E> newNode = new Node<>(pred, e, null);
  24. if (pred == null) //前驱为null.新结点为头结点
  25. first = newNode;
  26. else //否则新结点为前驱结点的next
  27. pred.next = newNode;
  28. pred = newNode; //前驱结点后移
  29. }
  30. if (succ == null) { //后继结点为null,前驱就为尾结点
  31. last = pred;
  32. } else { //目前的pred.next指向succ
  33. pred.next = succ;
  34. succ.prev = pred;
  35. }
  36. size += numNew; //改变长度
  37. modCount++;
  38. return true;
  39. }

Deque接口的添加操作

addFirst(E e)方法

  1. public void addFirst(E e) {
  2. linkFirst(e);
  3. }
  4. private void linkFirst(E e) {
  5. final Node<E> f = first; //获取首结点
  6. final Node<E> newNode = new Node<>(null, e, f); //新建结点,以头结点为后继结点
  7. first = newNode;
  8. if (f == null) //判断是否为空
  9. last = newNode; //头结点为空,新结点为尾结点
  10. else
  11. f.prev = newNode; //新结点作为首结点的前缀结点
  12. size++;
  13. modCount++;
  14. }

从上面代码看到,实现就是在头结点插入一个结点使新节点称为新节点,但是和linkLast一样需要注意当链表为空时,对last结点的设置。

addLast(E e)方法

addLast()方法用于将元素添加到链表尾部。

  1. public void addLast(E e) {
  2. linkLast(e);
  3. }

offer(E e)方法

offer()方法用于将数据添加到链表尾部,其内部调用了add(E e)方法

  1. public boolean offer(E e) {
  2. return add(e);
  3. }

offerFirst(E e)方法

offerFirst()方法将数据插入到链表头部,与addFirst的区别在于该方法可以返回特点的返回值,而addFirst的返回值为void

  1. public boolean offerFirst(E e) {
  2. addFirst(e);
  3. return true;
  4. }

获取任意位置的get(int index)方法

  1. public E get(int index) {
  2. checkElementIndex(index); //检查是否越界
  3. return node(index).item; //返回结点的数据
  4. }

获取位置为0的头结点位置

LinkedList中有多种方法可以获得头结点的数据,实现大同小异,区别在于对链表为空时的处理,是抛出异常还是返回null。主要方法有getFirst()、element()、peek()、peekFirst()。

  1. public E getFirst() {
  2. final Node<E> f = first;
  3. if (f == null)
  4. throw new NoSuchElementException();
  5. return f.item;
  6. }
  7. public E element() {
  8. return getFirst();
  9. }
  10. public E peek() {
  11. final Node<E> f = first;
  12. return (f == null) ? null : f.item;
  13. }
  14. public E peekFirst() {
  15. final Node<E> f = first;
  16. return (f == null) ? null : f.item;
  17. }
  18. public E peekLast() {
  19. final Node<E> l = last;
  20. return (l == null) ? null : l.item;
  21. }

根据对象得到索引

  1. public int indexOf(Object o) {
  2. int index = 0;
  3. if (o == null) { //从头往后遍历
  4. for (Node<E> x = first; x != null; x = x.next) {
  5. if (x.item == null)
  6. return index;
  7. index++;
  8. }
  9. } else { //从头往后遍历
  10. for (Node<E> x = first; x != null; x = x.next) {
  11. if (o.equals(x.item))
  12. return index;
  13. index++;
  14. }
  15. }
  16. return -1;
  17. }
  18. //从后往头遍历
  19. public int lastIndexOf(Object o) {
  20. int index = size;
  21. if (o == null) {
  22. for (Node<E> x = last; x != null; x = x.prev) {
  23. index--;
  24. if (x.item == null)
  25. return index;
  26. }
  27. } else {
  28. for (Node<E> x = last; x != null; x = x.prev) {
  29. index--;
  30. if (o.equals(x.item))
  31. return index;
  32. }
  33. }
  34. return -1;
  35. }

检查链表是否包含某对象

  1. public boolean contains(Object o) {
  2. return indexOf(o) != -1;
  3. }

删除指定对象

  1. public boolean remove(Object o) {
  2. //如果删除对象为null
  3. if (o == null) {
  4. //从前往后遍历
  5. for (Node<E> x = first; x != null; x = x.next) {
  6. if (x.item == null) {
  7. unlink(x); //调用unlink方法
  8. return true;
  9. }
  10. }
  11. } else {
  12. //从前往后遍历
  13. for (Node<E> x = first; x != null; x = x.next) {
  14. if (o.equals(x.item)) {
  15. unlink(x);
  16. return true;
  17. }
  18. }
  19. }
  20. return false;
  21. }
  22. E unlink(Node<E> x) {
  23. // assert x != null;
  24. final E element = x.item;
  25. final Node<E> next = x.next; //得到后继结点
  26. final Node<E> prev = x.prev; //得到前驱结点
  27. //删除前驱结点
  28. if (prev == null) {
  29. first = next; //如果删除的结点时头结点,令头结点指向该结点的后继结点
  30. } else {
  31. prev.next = next; //将前驱结点的后继指向后继结点
  32. x.prev = null;
  33. }
  34. //删除后继结点
  35. if (next == null) {
  36. last = prev; //如果删除的结点时是尾结点,令尾结点指向该结点的前驱结点
  37. } else {
  38. next.prev = prev;
  39. x.next = null;
  40. }
  41. x.item = null;
  42. size--;
  43. modCount++;
  44. return element;
  45. }

删除任意位置的对象

  1. public E remove(int index) {
  2. //检查范围
  3. checkElementIndex(index);
  4. //删除结点
  5. return unlink(node(index));
  6. }

删除头结点对象

  1. public E remove(){
  2. return removeFirst();
  3. }
  4. public E pop(){
  5. return removeFirst();
  6. }
  7. public E removeFirst(){
  8. final Node<E> f = first;
  9. if(f == null)
  10. throws new NoSuchElementException();
  11. return unlinkFirst(f);
  12. }
  13. public E poll(){
  14. final Node<E> f = first;
  15. return (f == null) ? null : unlinkFirst(f);
  16. }
  17. public E pollFirst(){
  18. final Node<E> f = first;
  19. return (f == null) ? null : unlinkFirst(f);
  20. }

删除尾结点对象

  1. public E removeLast() {
  2. final Node<E> l = last;
  3. if (l == null)
  4. throw new NoSuchElementException();
  5. return unlinkLast(l);
  6. }

总结:

LinkedList是基于双链表的List,其内部的实现源于对链表的操作,所以适用于频繁增加,删除的清理;另外,由于LinkedList实现了Queue接口,所以不只有队列的接口,还有栈的接口,还可以使用它作为队列和栈的实现。

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/843769
推荐阅读
相关标签
  

闽ICP备14008679号