类图结构
LinkedList是通过双向链表去实现的,他的数据结构具有双向链表结构的优缺点
既然是双向链表,那么它的顺序访问会非常高效,而随机访问效率比较低。
它包含一个非常重要的私有的静态内部类:Node。
private static class Node<E> {
E item; // 存储的元素
Node<E> next; // 下一个Node节点
Node<E> prev; // 前一个Node节点
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
重要的属性 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; // 链表的尾节点
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; // 链表的尾节点
构造方法 LinkedList linkedlist = new LinkedList();
/**
* Constructs an empty list.
* 默认为空
*/
public LinkedList() {
}
LinkedList linkedlist = new LinkedList();
/**
* Constructs an empty list.
* 默认为空
*/
public LinkedList() {
}
添加数据方式1
从头部添加
linkedlist.push(1);
源码
/**
* Pushes an element onto the stack represented by this list. In other
* words, inserts the element at the front of this list.
*
* <p>This method is equivalent to {@link #addFirst}.
*
* @param e the element to push
* @since 1.6
*/
public void push(E e) {
addFirst(e);
}
/**
* Inserts the specified element at the beginning of this list.
*
* @param e the element to add
*/
public void addFirst(E e) {
linkFirst(e);
}
/**
* 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++;
}
添加数据方式2
从尾部添加
linkedlist.add(2);
源码
/**
* Appends the specified element to the end of this list.
*
* <p>This method is equivalent to {@link #addLast}.
*
* @param e element to be appended to this list
* @return {@code true} (as specified by {@link Collection#add})
*/
public boolean add(E e) {
linkLast(e);
return true;
}
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++;
}
由于LinkedList实现了List的接口,所有必然具备List的特性.接下来看List接口中定义的一个方法get(int index)和set(int index,E e);
get(index) public E get(int index) {
checkElementIndex(index); // 检查下标是否合法
return node(index).item;
}
Node<E> node(int index) {
// assert isElementIndex(index);
// 判断index是否小于size的一半
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;
}
}
public E get(int index) {
checkElementIndex(index); // 检查下标是否合法
return node(index).item;
}
Node<E> node(int index) {
// assert isElementIndex(index);
// 判断index是否小于size的一半
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;
}
}
本质还是遍历链表中的数据
set(int index,E e) public E set(int index, E element) {
checkElementIndex(index); // 检查下标是否越界
Node<E> x = node(index); // 根据下标获取对应的Node节点
E oldVal = x.item; // 记录原来的值
x.item = element; // 修改
return oldVal; // 返回原来的值
}
public E set(int index, E element) {
checkElementIndex(index); // 检查下标是否越界
Node<E> x = node(index); // 根据下标获取对应的Node节点
E oldVal = x.item; // 记录原来的值
x.item = element; // 修改
return oldVal; // 返回原来的值
}