Said front
Before finishing off information on a one-way linked list: https://blog.csdn.net/Lzinner/article/details/94838953 , two-way and one-way linked list linked list is very similar, so this one will learn a lot of things on a content
One-way analysis of the shortcomings of the list:
1. singly linked list, the search direction is only one direction, and doubly linked list can be forward or backward to find
2. singly linked list can not delete itself , it needs the secondary node (temp aid is to be deleted before a node node), and doubly linked list, you can delete themselves
3. Illustration:
About traverse the doubly linked list, add, modify, delete
1. traversal:
* consistent and singly linked list
//遍历双向链表的方法
public void showList() {
if (head.next == null) {
System.out.println("链表为空");
return;
}
//因为头节点不能动,因此我们需要一个辅助变量来遍历
HeroNode2 tempNode = head;
while (true) {
//是否到链表的最后
if (tempNode.next == null) {
break;
}
//输出节点的信息
System.out.println(tempNode.next);
tempNode = tempNode.next;
}
}
2. Add the
* first find the penultimate node-way linked list (temp.next == null), then add
//添加一个节点到双向链表的最后
public void add(HeroNode2 heroNode) {
//因为head节点不能动,因此需要一个辅助变量
HeroNode2 temp = head;
//遍历链表
while (true) {
//todo HeroNode temp=head if(temp.next==null)
//当temp为空 说明找到最后了
if (temp.next == null) {//
break;
}
//未找到 temp后移
temp = temp.next;
}
//当退出while循环时,temp指向了链表的最后
//关联双向链表
temp.next = heroNode;
heroNode.pre = temp;
}
3. Modify node:
* singly linked list and consistent
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;
}
if (flag) {
//已经找到了
temp.name = newHeroNode.name;
temp.nickName = newHeroNode.nickName;
} else {
System.out.printf("没有找到 编号%d 的节点,不能修改\n", newHeroNode.no);
}
}
4. Remove nodes
* in the singly linked list, it is necessary to find a node before the node to be deleted, can be deleted through the method temp.next.next of temp.next =
* In the doubly linked list, only need to find a node to be deleted It can then delete itself by way of
self-removing code
**tempNode.pre.next = tempNode.next;
//有风险
// 如果要删除最后一个节点,就不需要执行下面这句话,否则会出现空指针异常
//tempNote.next=null tempNote.next.pre
if(tempNode.next!=null){
tempNode.next.pre = tempNode.pre;
}**
public void deleteNode(int no) {
if (head.next == null) {
//链表为空
System.out.println("链表为空,无法删除");
return;
}
HeroNode2 tempNode = head.next;//辅助节点
boolean flag = false;
while (true) {
if (tempNode == null) {
System.out.println("遍历结束");
break;
}
if (tempNode.no == no) {
//找到了该节点
//进行节点的删除
flag = true;
break;
}
tempNode = tempNode.next;
}
if (flag) {
//可以删除了
tempNode.pre.next = tempNode.next;
//有风险
// 如果要删除最后一个节点,就不需要执行下面这句话,否则会出现空指针异常
//tempNote.next=null tempNote.next.pre
if (tempNode.next != null) {
tempNode.next.pre = tempNode.pre;
}
} else {
System.out.printf("没有找到 编号%d 的节点,不能修改\n", no);
}
}
The complete code for doubly linked list operations:
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(hero2);
doubleLinkedList.add(hero3);
doubleLinkedList.add(hero4);
doubleLinkedList.showList();
System.out.println("------------------------------------------");
//修改
HeroNode2 hero_modify = new HeroNode2(4, "公孙胜", "入云龙");
doubleLinkedList.update(hero_modify);
System.out.println("修改后的链表");
doubleLinkedList.showList();
//测试删除功能
doubleLinkedList.deleteNode(3);
System.out.println("删除后的链表");
doubleLinkedList.showList();
}
}
class DoubleLinkedList {
//先初始化一个头节点,不存放具体的数值
private HeroNode2 head = new HeroNode2(0, "", "");
public HeroNode2 getHead() {
return head;
}
public void setHead(HeroNode2 head) {
this.head = head;
}
//遍历双向链表的方法
public void showList() {
if (head.next == null) {
System.out.println("链表为空");
return;
}
//因为头节点不能动,因此我们需要一个辅助变量来遍历
HeroNode2 tempNode = head;
while (true) {
//是否到链表的最后
if (tempNode.next == null) {
break;
}
//输出节点的信息
System.out.println(tempNode.next);
tempNode = tempNode.next;
}
}
//添加一个节点到双向链表的最后
public void add(HeroNode2 heroNode) {
//因为head节点不能动,因此需要一个辅助变量
HeroNode2 temp = head;
//遍历链表
while (true) {
//todo HeroNode temp=head if(temp.next==null)
//当temp为空 说明找到最后了
if (temp.next == null) {//
break;
}
//未找到 temp后移
temp = temp.next;
}
//当退出while循环时,temp指向了链表的最后
//关联双向链表
temp.next = heroNode;
heroNode.pre = temp;
}
//修改一个节点的内容,双向链表修改节点和单向链表代码一致
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;
}
if (flag) {
//已经找到了
temp.name = newHeroNode.name;
temp.nickName = newHeroNode.nickName;
} else {
System.out.printf("没有找到 编号%d 的节点,不能修改\n", newHeroNode.no);
}
}
//从双向链表中删除一个节点
/**
* 对于双向链表,可以直接找到要删除的这个节点
* 找到后,自我删除即可
*/
public void deleteNode(int no) {
if (head.next == null) {
//链表为空
System.out.println("链表为空,无法删除");
return;
}
HeroNode2 tempNode = head.next;//辅助节点
boolean flag = false;
while (true) {
if (tempNode == null) {
System.out.println("遍历结束");
break;
}
if (tempNode.no == no) {
//找到了该节点
//进行节点的删除
flag = true;
break;
}
tempNode = tempNode.next;
}
if (flag) {
//可以删除了
tempNode.pre.next = tempNode.next;
//有风险
// 如果要删除最后一个节点,就不需要执行下面这句话,否则会出现空指针异常
//tempNote.next=null tempNote.next.pre
if (tempNode.next != null) {
tempNode.next.pre = tempNode.pre;
}
} else {
System.out.printf("没有找到 编号%d 的节点,不能修改\n", no);
}
}
}
class HeroNode2 {
public int no;
public String name;
public String nickName;
public HeroNode2 next;//指向下一个节点,默认为null
public HeroNode2 pre; //指向前一个节点,默认为null
//构造器
public HeroNode2(int hNo, String hName, String hNickName) {
this.no = hNo;
this.name = hName;
this.nickName = hNickName;
}
@Override
public String toString() {
return "HeroNode2{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}