这次是自己阅读JDK源码得到的一些顿悟,java集合类LinkList是数据结构链表的实现。
LinkedList继承了AbstractSequentiaList,主要实现了接口List里的方法。
public class LinkedListextends AbstractSequentialListimplements List, Deque, Cloneable, java.io.Serializable
链表头节点:
transient Node first;
链表表尾节点:
transient Node last;
1.1添加节点
使用前插法创建链表:每添加一个节点,都会在当前的链表头部添加一个节点,然后添加之后的节点成为这个链表的头部---(先进为尾,后进为头)。
public void addFirst(E e) {
linkFirst(e);
}
private void linkFirst(E e) {
final Node f = first;
final Node newNode =new Node<>(null, e, f);
first = newNode;
if (f ==null)
last = newNode;
else
f.prev = newNode;
size++;
modCount++;}
使用后插法创建链表:当有节点添加进链表时,都会在当前链表的尾部添加该节点,链表add()方法默认时链表的后插法。---(先进为头,后进为尾)。
public void addLast(E e) {
linkLast(e);
}
public boolean add(E e) {
linkLast(e);
return true;
}
void linkLast(E e) {
final Node l = last;
final Node newNode =new Node<>(l, e, null);
last = newNode;
if (l ==null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
1.2删除节点---删节点动作之前都要做判空处理。以免删空链表导致程序报错
删除头节点:将当前链表头的next所指向的地址,赋予给first节点。之后再将该节点的前缀赋予null。
public E removeFirst() {
final Node f = first;
if (f ==null)throw new NoSuchElementException();
return unlinkFirst(f);
}
private E unlinkFirst(Node f) {
// assert f == first && f != null;
final E element = f.item;
final Node 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;}
删除链表尾节点:找出链表表尾的前缀所指向的地址,之后就是将原先未处理的链表表尾所指节点的next指针置为空。达到删除表尾元素的效果。
public E removeLast() {
final Node l = last;
if (l ==null)throw new NoSuchElementException();
return unlinkLast(l);
}
private E unlinkLast(Node l) {
// assert l == last && l != null;
final E element = l.item;
final Node 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;
}
删除特定节点:
public E remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}
*删除链表中的节点:包括前两种处理。是remove()方法的实现。获取要删除节点的prev所指节点和next做直接点。如果prev所指的节点为null,则表示要删除的节点是头结点,操作和removeFirst()相一致,如果是链表表尾节点,操作和removeLast()相一致。非上面两种情况,则会将的prev所指节点指向next所致的节点。
public boolean remove(Object o) {
if (o ==null) {
for (Node x = first; x !=null; x = x.next) {
if (x.item ==null) {
unlink(x);
return true;
}
}
}else {
for (Node x = first; x !=null; x = x.next) {
if (o.equals(x.item)) {
unlink(x);
return true;
}
}
}
return false;}
E unlink(Node x) {
// assert x != null;
final E element = x.item;
final Node next = x.next;
final Node 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;
}
1.3添加所有节点:使用一个过度节点newNode来组装该链表。
public boolean addAll(Collection c) {return addAll(size, c);}
public boolean addAll(int index, Collection c) {
checkPositionIndex(index);
Object[] a = c.toArray();
int numNew = a.length;
if (numNew ==0)return false;
Node pred, succ;
if (index == size) {
succ =null;
pred = last;
}else {
succ = node(index);
pred = succ.prev;
}
for (Object o : a) {
@SuppressWarnings("unchecked") E e = (E) o;
Node newNode =new Node<>(pred, e, null);
if (pred ==null)
first = newNode;
else
pred.next = newNode;
pred = newNode;
}
if (succ ==null) {
last = pred;
} else {
pred.next = succ;
succ.prev = pred;
}
size += numNew;
modCount++;
return true;
}
1.4修改链表中特定的节点:
public E set(int index, E element) {
checkElementIndex(index);
Node x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
1.5在特定的位置添加节点:
public void add(int index, E element) {
checkPositionIndex(index);
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}
void linkBefore(E e, Node succ) {
// assert succ != null;
final Node pred = succ.prev;
final Node newNode =new Node<>(pred, e, succ);
succ.prev = newNode;
if (pred ==null)
first = newNode;
else
pred.next = newNode;
size++;
modCount++;
}
定位节点位置:
Node node(int index) {
// assert isElementIndex(index);
if (index < (size >>1)) {
Node x = first;
for (int i =0; i < index; i++)
x = x.next;
return x;
}else {
Node x = last;
for (int i = size -1; i > index; i--)
x = x.prev;
return x; }}
1.6获取特定节点信息:
public E get(int index) { checkElementIndex(index); return node(index).item;}
1.7清空链表信息:
public void clear() {
// Clearing all of the links between nodes is "unnecessary", but:
// - helps a generational GC if the discarded nodes inhabit
// more than one generation
// - is sure to free memory even if there is a reachable Iterator
for (Node x = first; x !=null; ) {
Node next = x.next;
x.item =null;
x.next =null;
x.prev =null;
x = next;
}
first = last =null;
size =0;
modCount++;
}
平常在工作使用的比较多的,链表中的add(),addAll(),remove()get(),clear()方法的具体实现逻辑。笔者第一次写的博客,还有很多不足的地方,希望看过的博主们能提一提您宝贵的建议。
笔者目前是一个java小菜鸟,还在努力学习和成长中。技术的进步贵在交流。