Doubly linked list:
Set a pointer field to the predecessor node in each node of the singly linked list.
Example:
Node structure:
Advantages:
The main advantage of a doubly linked list is that for any given node, it can be easily obtained. The former node and the latter node
realize two-way search (single-linked list is not easy to do)
. The position i in the table can be represented by a pointer indicating that it contains the i-th node.
Disadvantages:
large space overhead, each node needs to save two attributes next and prev, so more space overhead is needed, and node insertion and deletion operations will also be more time-consuming, because more pointing operations are required.
Insert operation diagram:
delete operation diagram:
code implementation:
define a doubly linked list structure as follows:
/ 定义HeroNode2 , 每个HeroNode 对象就是一个节点
class HeroNode2 {
public int no;
public String name;
public String nickname;
public HeroNode2 next; // 指向下一个节点, 默认为null
public HeroNode2 pre; // 指向前一个节点, 默认为null
// 构造器
public HeroNode2(int no, String name, String nickname) {
this.no = no;
this.name = name;
this.nickname = nickname;
}
// 为了显示方法,我们重新toString
@Override
public String toString() {
return "HeroNode [no=" + no + ", name=" + name + ", nickname=" + nickname + "]";
}
}
1. Add a node to the end of the doubly linked list
// 添加一个节点到双向链表的最后.
public void add(HeroNode2 heroNode) {
// 因为head节点不能动,因此我们需要一个辅助遍历 temp
HeroNode2 temp = head;
// 遍历链表,找到最后
while (true) {
// 找到链表的最后
if (temp.next == null) {
//
break;
}
// 如果没有找到最后, 将将temp后移
temp = temp.next;
}
// 当退出while循环时,temp就指向了链表的最后
// 形成一个双向链表
temp.next = heroNode;
heroNode.pre = temp;
}
2. Insert nodes in order, that is, insert into the middle or end of the doubly linked list
//按序插入节点,即插入双向链表的中间
public void addByOrder(HeroNode2 heroNode){
// 因为头节点,不能动,因此我们需要一个辅助变量来遍历
HeroNode2 temp = head;
// 遍历链表,找到最插入的位置
boolean flag = false; // flag标志添加的编号是否存在,默认为false
while (true) {
if (temp.next == null) {
//说明链表已经遍历完了
break;
}
if (temp.next.no > heroNode.no) {
//找到位置
break;
} else if (temp.no == heroNode.no) {
//说明希望添加的编号已经存在
flag = true;
break;
}
temp = temp.next;//后移
}
if (flag) {
System.out.printf("准备插入的节点编号%d已经存在 ,不能在加入", heroNode.no);
} else {
//插入到链表中,temp的后面
heroNode.next = temp.next;
if (temp.next!=null) {
//插入再中间,注意空指针,如果不是插入在中间,temp.next.pre会出现空指针异常
temp.next.pre = heroNode;
}
heroNode.pre = temp;
temp.next = heroNode;
}
}
3. Modify the content of a node, you can see that the modification of the node content of the doubly linked list is the same as that of the singly linked list
// 修改一个节点的内容, 可以看到双向链表的节点内容修改和单向链表一样
// 只是 节点类型改成 HeroNode2
public void update(HeroNode2 newHeroNode) {
// 判断是否空
if (head.next == null) {
System.out.println("链表为空~");
return;
}
// 找到需要修改的节点, 根据no编号
// 定义一个辅助变量
HeroNode2 temp = head.next;
boolean flag = false; // 表示是否找到该节点
while (true) {
if (temp == null) {
break; // 已经遍历完链表
}
if (temp.no == newHeroNode.no) {
// 找到
flag = true;
break;
}
temp = temp.next;
}
// 根据flag 判断是否找到要修改的节点
if (flag) {
temp.name = newHeroNode.name;
temp.nickname = newHeroNode.nickname;
} else {
// 没有找到
System.out.printf("没有找到 编号 %d 的节点,不能修改\n", newHeroNode.no);
}
}
4. Delete a node from the doubly linked list.
Note
1 For the doubly linked list, we can directly find the node to be deleted.
2 After finding it, we can delete it by ourselves
temp.pre.next = temp.next;
temp.next.pre = temp .pre;
// 从双向链表中删除一个节点,
// 说明
// 1 对于双向链表,我们可以直接找到要删除的这个节点
// 2 找到后,自我删除即可
// temp.pre.next = temp.next;
//temp.next.pre = temp.pre;
public void del(int no) {
// 判断当前链表是否为空
if (head.next == null) {
// 空链表
System.out.println("链表为空,无法删除");
return;
}
HeroNode2 temp = head.next; // 辅助变量(指针)
boolean flag = false; // 标志是否找到待删除节点的
while (true) {
if (temp == null) {
// 已经到链表的最后
break;
}
if (temp.no == no) {
// 找到的待删除节点的前一个节点temp
flag = true;
break;
}
temp = temp.next; // temp后移,遍历
}
// 判断flag
if (flag) {
// 找到
// 可以删除
// temp.next = temp.next.next;[单向链表]
temp.pre.next = temp.next;
// 这里我们的代码有问题?
// 如果是最后一个节点,就不需要执行下面这句话,否则出现空指针
if (temp.next != null) {
temp.next.pre = temp.pre;
}
} else {
System.out.printf("要删除的 %d 节点不存在\n", no);
}
}
Complete code:
public class DoubleLinkedListDemo {
public static void main(String[] args) {
System.out.println("双向链表的测试");
// 先创建节点
HeroNode2 hero1 = new HeroNode2(1, "宋江", "及时雨");
HeroNode2 hero2 = new HeroNode2(2, "卢俊义", "玉麒麟");
HeroNode2 hero3 = new HeroNode2(3, "吴用", "智多星");
HeroNode2 hero4 = new HeroNode2(4, "林冲", "豹子头");
// 创建一个双向链表
DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
//doubleLinkedList.add(hero1);
//doubleLinkedList.add(hero4);
//doubleLinkedList.add(hero3);
//doubleLinkedList.add(hero2);
doubleLinkedList.addByOrder(hero1);
doubleLinkedList.addByOrder(hero4);
doubleLinkedList.addByOrder(hero3);
doubleLinkedList.addByOrder(hero2);
doubleLinkedList.list();
// 修改
HeroNode2 newHeroNode = new HeroNode2(4, "公孙胜", "入云龙");
doubleLinkedList.update(newHeroNode);
System.out.println("修改后的链表情况");
doubleLinkedList.list();
// 删除
doubleLinkedList.del(3);
System.out.println("删除后的链表情况~~");
doubleLinkedList.list();
}
}
// 创建一个双向链表的类
class DoubleLinkedList {
// 先初始化一个头节点, 头节点不要动, 不存放具体的数据
private HeroNode2 head = new HeroNode2(0, "", "");
// 返回头节点
public HeroNode2 getHead() {
return head;
}
// 遍历双向链表的方法
// 显示链表[遍历]
public void list() {
// 判断链表是否为空
if (head.next == null) {
System.out.println("链表为空");
return;
}
// 因为头节点,不能动,因此我们需要一个辅助变量来遍历
HeroNode2 temp = head.next;
while (true) {
// 判断是否到链表最后
if (temp == null) {
break;
}
// 输出节点的信息
System.out.println(temp);
// 将temp后移, 一定小心
temp = temp.next;
}
}
// 添加一个节点到双向链表的最后.
public void add(HeroNode2 heroNode) {
// 因为head节点不能动,因此我们需要一个辅助遍历 temp
HeroNode2 temp = head;
// 遍历链表,找到最后
while (true) {
// 找到链表的最后
if (temp.next == null) {
//
break;
}
// 如果没有找到最后, 将将temp后移
temp = temp.next;
}
// 当退出while循环时,temp就指向了链表的最后
// 形成一个双向链表
temp.next = heroNode;
heroNode.pre = temp;
}
//按序插入节点,即插入双向链表的中间
public void addByOrder(HeroNode2 heroNode){
// 因为头节点,不能动,因此我们需要一个辅助变量来遍历
HeroNode2 temp = head;
// 遍历链表,找到最插入的位置
boolean flag = false; // flag标志添加的编号是否存在,默认为false
while (true) {
if (temp.next == null) {
//说明链表已经遍历完了
break;
}
if (temp.next.no > heroNode.no) {
//找到位置
break;
} else if (temp.no == heroNode.no) {
//说明希望添加的编号已经存在
flag = true;
break;
}
temp = temp.next;//后移
}
if (flag) {
System.out.printf("准备插入的节点编号%d已经存在 ,不能在加入", heroNode.no);
} else {
//插入到链表中,temp的后面
heroNode.next = temp.next;
if (temp.next!=null) {
//插入再中间,注意空指针,如果不是插入在中间,temp.next.pre会出现空指针异常
temp.next.pre = heroNode;
}
heroNode.pre = temp;
temp.next = heroNode;
}
}
// 修改一个节点的内容, 可以看到双向链表的节点内容修改和单向链表一样
// 只是 节点类型改成 HeroNode2
public void update(HeroNode2 newHeroNode) {
// 判断是否空
if (head.next == null) {
System.out.println("链表为空~");
return;
}
// 找到需要修改的节点, 根据no编号
// 定义一个辅助变量
HeroNode2 temp = head.next;
boolean flag = false; // 表示是否找到该节点
while (true) {
if (temp == null) {
break; // 已经遍历完链表
}
if (temp.no == newHeroNode.no) {
// 找到
flag = true;
break;
}
temp = temp.next;
}
// 根据flag 判断是否找到要修改的节点
if (flag) {
temp.name = newHeroNode.name;
temp.nickname = newHeroNode.nickname;
} else {
// 没有找到
System.out.printf("没有找到 编号 %d 的节点,不能修改\n", newHeroNode.no);
}
}
// 从双向链表中删除一个节点,
// 说明
// 1 对于双向链表,我们可以直接找到要删除的这个节点
// 2 找到后,自我删除即可
// temp.pre.next = temp.next;
//temp.next.pre = temp.pre;
public void del(int no) {
// 判断当前链表是否为空
if (head.next == null) {
// 空链表
System.out.println("链表为空,无法删除");
return;
}
HeroNode2 temp = head.next; // 辅助变量(指针)
boolean flag = false; // 标志是否找到待删除节点的
while (true) {
if (temp == null) {
// 已经到链表的最后
break;
}
if (temp.no == no) {
// 找到的待删除节点的前一个节点temp
flag = true;
break;
}
temp = temp.next; // temp后移,遍历
}
// 判断flag
if (flag) {
// 找到
// 可以删除
// temp.next = temp.next.next;[单向链表]
temp.pre.next = temp.next;
// 这里我们的代码有问题?
// 如果是最后一个节点,就不需要执行下面这句话,否则出现空指针
if (temp.next != null) {
temp.next.pre = temp.pre;
}
} else {
System.out.printf("要删除的 %d 节点不存在\n", no);
}
}
}
// 定义HeroNode2 , 每个HeroNode 对象就是一个节点
class HeroNode2 {
public int no;
public String name;
public String nickname;
public HeroNode2 next; // 指向下一个节点, 默认为null
public HeroNode2 pre; // 指向前一个节点, 默认为null
// 构造器
public HeroNode2(int no, String name, String nickname) {
this.no = no;
this.name = name;
this.nickname = nickname;
}
// 为了显示方法,我们重新toString
@Override
public String toString() {
return "HeroNode [no=" + no + ", name=" + name + ", nickname=" + nickname + "]";
}
}