您的位置:首页 > 编程语言 > Java开发

java LinkedList

2016-07-16 17:33 405 查看
快下班了,还有一个小时,赶快再写一篇博客。已经把下周要干的工作编好了。刚才接了个电话,让我投资写字楼,太睢得起我了,我很low啊。。。费话不说,先看代码,
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
transient int size = 0;

/**
* Pointer to first node.
* Invariant: (first == null && last == null) ||
*            (first.prev == null && first.item != null)
*/
transient Node<E> first;

/**
* Pointer to last node.
* Invariant: (first == null && last == null) ||
*            (last.next == null && last.item != null)
*/
transient Node<E> last;
}
有点吃惊,妈蛋的,怎么和别人分析的不一样,这个Node是什么鬼,first和last是什么玩意,别的人Entry和header的啊,为什么到我这里就变了。管它哪,先胡乱分析一下,把这个博客写完了再说。这里明显有一个地方我看不懂,那就是Node,是个什么玩意儿,我找一找代码,把内容贴上来。
private static class Node<E> {
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;
}
}
看到代码了吧,这个Node就是一个里面装元素的item,一个是指定后面节点的next,一个指定前面节点的prev,简单的要死,这个就不是实现双向链表的一个典型的节点嘛。哥们,只要你读过数据结构,这个还是看得出来的吧。我现在知道Node是什么了,那么我就可以大胆的猜了。最上面这个LinkedList,这个成员变量size就是指这个链表有多长的吧。怎么证明我猜的对,我找一个size()方法,如果直接返回的是size成员变量,我不就猜对了么。等下,我去找一找。。。
public int size() {
return size;
}
找到了,看到了没有,真的是这样的。这个成员变量搞清楚了,那么first和last是什么东东?一看名字我都又开始了我的猜测大法了,谁让起这个名字的。first是指链表的第一个Node,last是最后一个Node,我就是这么敢猜。如何证明我想的嘞,那么我想,这个链表添加元素的时候,肯定要搞点什么吧,我找找有没有相关的方法。
/**
* Appends the specified element to the end of this list.
*/
public boolean add(E e) {
linkLast(e);
return true;
}

/**
* Links e as last element.
*/
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++;
}

Node(LinkedList.Node<E> prev, E element, LinkedList.Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
看到了如何给尾部追加一个Node节点了吧,没有问题,first和last的问题解决了。但是现在我好奇,LinkdedList的优势应该是中间插入一个元素啊,我想知道插入一个元素是如何做到的,不要急,等我去找一下代码
/**
* Inserts the specified element at the specified position in this list.
* Shifts the element currently at that position (if any) and any
* subsequent elements to the right (adds one to their indices).
*/
public void add(int index, E element) {
checkPositionIndex(index);

if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}

/**
* Returns the (non-null) Node at the specified element index.
*/
LinkedList.Node<E> node(int index) {
// assert isElementIndex(index);

if (index < (size >> 1)) {
LinkedList.Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
LinkedList.Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}

/**
* Inserts element e before non-null Node succ.
*/
void linkBefore(E e, LinkedList.Node<E> succ) {
// assert succ != null;
final LinkedList.Node<E> pred = succ.prev;
final LinkedList.Node<E> newNode = new LinkedList.Node<>(pred, e, succ);
succ.prev = newNode;
if (pred == null)
first = newNode;
else
pred.next = newNode;
size++;
modCount++;
}
找到了,看了一下,简单说一下就是,要在一个位置插入一个元素,那么先找到这个位置上的节点Node,然后在这个节点前面插入一个新节点就可以了,新节点也不是天上来的,new的,然后原来位置节点上的prev指向新节点,原来节点的prev节点的next指向新节点,就over了。在这之前,new 新节点的时候,构造函数完成了新节点的prev和next的指向。下面再看一下删除指定位置上的节点的方法,增加一点情趣
/**
* Removes the element at the specified position in this list.  Shifts any
* subsequent elements to the left (subtracts one from their indices).
* Returns the element that was removed from the list.
*/
public E remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}

/**
* Returns the (non-null) Node at the specified element index.
*/
LinkedList.Node<E> node(int index) {
// assert isElementIndex(index);

if (index < (size >> 1)) {
LinkedList.Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
LinkedList.Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}

/**
* Unlinks non-null node x.
*/
E unlink(LinkedList.Node<E> x) {
// assert x != null;
final E element = x.item;
final LinkedList.Node<E> next = x.next;
final LinkedList.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;
}
从上面来看,基本差不太多,还是先到指定位置的节点,我们把它叫x吧,前面的叙述中我没有起名字,写的自己都晕了。讲真的,这个博客写完以后,我自己都不想再看了,因为写的无任何营养。调用x=node(index),得到了x,然后调用unlink(x),删除这个节点。unlink干的事儿就是常规的了。前三名话的意思简单,第一句就是保存这个元素,因为后面要return用。第二句是保存x的next节点的位置,第三名是保存x的prev节点的位置。然后如果prev是null,就说明x是他妈的first啊,这个时候你要把first删除了,是不是后面的next要顶上来当first,就是first
= next啦。如果prev!=null,明显x不是first,那么简单,就是prev.next = next,跳过x,然后把x.prev=null空,方便回收资源。然后同理,如果next=null,明显x就是last啊,那么last = prev;这个是要顶上的,对吧。如果不是最后一个节点,同理,next.prev=prev,跳过x,然后再把x.next=null,回收资源。然后再把x.item=null,不要这个内容了。这样链表的大小明显小了1,就是size--了。modCount++说明对链表进行了修改,方便快速迭代失败。然后再返回删除的这个元素element就可以了。总体来讲,这个LinkedList就是我心目中的list,和我学数据结构的想法差不多,缺点和优点用屁股一想就能想到,不复杂,以后使用的时候看一下源码就能明白。我知道我写这个屁文对别人一点儿帮助也没有,我一点儿也不愧疚,我本身就是制造垃圾的王中王!!!

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: