双端链表
对于单项链表,我们如果想在尾部添加一个节点,那么必须从头部一直遍历到尾部,找到尾节点,然后在尾节点后面插入一个节点。这样操作很麻烦,如果我们在设计链表的时候多个对尾节点的引用,那么会简单很多。
1、什么时双端链表:
链表中保持这对最后一个连点引用的链表
2、从头部插入
要对链表进行判断,如果为空则设置尾节点为新添加的节点
3、从尾部进行插入
如果链表为空,则直接设置头节点为新添加的节点,否则设置尾节点的后一个节点为新添加的节点
4、从头部删除
判断节点是否有下个节点,如果没有则设置节点为null
/**
* @Auther: liuhaidong
* Data: 2019/10/11 0011、12:07
* Description:
* @version: 1.0
*/
public class DoublePointLinkedList {
private Node head;
//头节点
private Node tail;
//尾节点
private int size;
//节点的个数
public DoublePointLinkedList() {
size = 0;
head = null;
tail = null;
}
//表头新增节点
public void addHead(int data){
Node node = new Node(data);
if (size == 0) {
//如果链表为空,那么头节点和尾节点都是该新增节点
head = node;
tail = node;
size++;
} else {
node.next = head;
head = node;
siz