Java数据结构--跟着api学数据结构--LinkedList
来源:互联网 发布:手机数据网络改dns劫持 编辑:程序博客网 时间:2024/05/20 18:51
LinkedList:双向链表实现
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; /** * Constructs an empty list. */ public LinkedList() { } /** * Constructs a list containing the elements of the specified * collection, in the order they are returned by the collection's * iterator. * * @param c the collection whose elements are to be placed into this list * @throws NullPointerException if the specified collection is null */ public LinkedList(Collection<? extends E> c) { this(); addAll(c); }
Node:
private static class Node<E> { E item; Node<E> next;<pre name="code" class="java"> /** * Links e as first element. */ private void linkFirst(E e) { final Node<E> f = first; final Node<E> newNode = new Node<>(null, e, f); first = newNode; if (f == null) last = newNode; else f.prev = newNode; size++; modCount++; } /** * 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++; } /** * Inserts element e before non-null Node succ. */ 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++; }
Node<E> prev; Node(Node<E> prev, E element, Node<E> next) { this.item = element; this.next = next; this.prev = prev; } }
/**
* Unlinks non-null first node f.
*/
private E unlinkFirst(Node<E> f) {
// assert f == first && f != null;
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;
}
/**
* Unlinks non-null last node l.
*/
private E unlinkLast(Node<E> l) {
// assert l == last && l != null;
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;
}
/**
* Unlinks non-null node x.
*/
E unlink(Node<E> x) {
// assert x != null;
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;
}
public boolean remove(Object o) {
if (o == null) {
for (Node<E> x = first; x != null; x = x.next) {
if (x.item == null) {
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;
}
/**
* Returns the (non-null) Node at the specified element index.
*/
Node<E> node(int index) {//通过链表遍历到特定的元素
// assert isElementIndex(index);
if (index < (size >> 1)) {
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
0 0
- Java数据结构--跟着api学数据结构--LinkedList
- Java数据结构--跟着api学数据结构--arraylist
- Java数据结构--跟着api学数据结构--Vector
- java中的数据结构--LinkedList
- Java数据结构-LinkedList
- 用java源代码学数据结构<四>: LinkedList 详解
- 数据结构(LinkedList的java实现)
- 数据结构--LinkedList的java实现
- 数据结构-Java实现-ArrayList&LinkedList
- JAVA数据结构-LinkedList,ArrayList,Tree,
- Java数据结构源码分析-LinkedList
- Java数据结构之手写LinkedList
- java数据结构之链表LinkedList
- java 利用 LinkedList类实现 数据结构 栈.......
- Java数据结构------ArrayList和LinkedList的区别
- Java数据结构-LinkedList(四)
- Java数据结构-线性表之单链表LinkedList
- 数据结构: Java中LinkedList的简单实现
- Java开发环境配置时的dt.jar与tools.jar是什么
- poj3050 Hopscotch(dfs基础)
- 《数据结构》第三章:栈和队列——>总结
- Impala:新一代开源大数据分析引擎
- Android的SQLite学习及使用方法
- Java数据结构--跟着api学数据结构--LinkedList
- st2---cocos2dx
- Word Break II
- 汇编器源码剖析
- emacs在mac上使用
- 百度实习面试
- LeetCode——Count and Say
- Leetcode: Trapping Rain Water
- git reset-----git 本地仓库回退