LinkedList源码分析
程序员文章站
2022-07-13 15:59:19
...
http://blog.csdn.net/zhouyong0/article/details/6427211
数据结构(LinkedList是双向循环链表)
1. 构造方法
/**
* Constructs an empty list.//构造一个空的列表
*/
public LinkedList() {
header.next = header.previous = header;
}
private transient Entry<E> header = new Entry<E>(null, null, null);
Entry(E element, Entry<E> next, Entry<E> previous) {//节点的构造函数
this.element = element; //元素的值
this.next = next; //后驱
this.previous = previous; //前驱
}
}
2. 函数add
/**
* 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 <tt>true</tt> (as specified by {@link Collection#add})
*/
public boolean add(E e) {
addBefore(e, header);
return true;
}
private Entry<E> addBefore(E e, Entry<E> entry) {
Entry<E> newEntry = new Entry<E>(e, entry, entry.previous); //创建一个新的节点
newEntry.previous.next = newEntry; //新的节点的前面一个元素的后驱是自己
newEntry.next.previous = newEntry; //新的节点的后面一个元素的前驱是自己
size++;
modCount++;
return newEntry;
}
另一个网址:
数据结构(LinkedList是双向循环链表)
1. 构造方法
/**
* Constructs an empty list.//构造一个空的列表
*/
public LinkedList() {
header.next = header.previous = header;
}
private transient Entry<E> header = new Entry<E>(null, null, null);
Entry(E element, Entry<E> next, Entry<E> previous) {//节点的构造函数
this.element = element; //元素的值
this.next = next; //后驱
this.previous = previous; //前驱
}
}
2. 函数add
/**
* 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 <tt>true</tt> (as specified by {@link Collection#add})
*/
public boolean add(E e) {
addBefore(e, header);
return true;
}
private Entry<E> addBefore(E e, Entry<E> entry) {
Entry<E> newEntry = new Entry<E>(e, entry, entry.previous); //创建一个新的节点
newEntry.previous.next = newEntry; //新的节点的前面一个元素的后驱是自己
newEntry.next.previous = newEntry; //新的节点的后面一个元素的前驱是自己
size++;
modCount++;
return newEntry;
}
另一个网址:
引用
http://blog.csdn.net/zl3450341/article/details/6025714
上一篇: v-chart x轴和y轴文本配置
下一篇: davinci等开源代码的前后部署