一、双向链表(Double Linked List)的修改、删除、查询及按编号顺序添加节点到双向链表的代码示例
1、定义一个人员节点类,每一个PersonNode对象就是一个节点
package com.rf.springboot01.dataStructure.doubleLinkedList2;
/**
* @description: 定义一个人员节点类,每一个PersonNode对象就是一个节点
* @author: xiaozhi
* @create: 2020-07-22 21:22
*/
public class PersonNode3 {
public int num;//编号
public String name;//名称
public String aliasName;//别名
public PersonNode3 next;//指向下一个节点,默认为null
public PersonNode3 pre;//指前一个节点,默认为null
//构造器
public PersonNode3(int num, String name, String aliasName) {
this.num = num;
this.name = name;
this.aliasName = aliasName;
}
//重写toString方法
@Override
public String toString() {
return "PersonNode{" +
"num=" + num +
", name='" + name + '\'' +
", aliasName='" + aliasName + '\'' +
'}';
}
}
2、创建一个双向链表管理人员节点
package com.rf.springboot01.dataStructure.doubleLinkedList2;
/**
* @description: 创建一个双向链表管理人员节点
* @author: xiaozhi
* @create: 2020-07-22 22:16
*/
public class DoubleLinkedList3 {
//先初始化一个头结点,头节点位置固定,不存放任何数据,作用是表示链表的头节点
private PersonNode3 head =new PersonNode3(0,"","");
public PersonNode3 getHead() {
return head;
}
/**
* @Description: 按编号顺序添加节点到双向链表
* @Param:
* @Author: xz
* @return:
* @Date: 2020/7/22 21:28
*/
public void insertDoubleLinkedList(PersonNode3 personNode3){
PersonNode3 temp=head;
boolean flag=false;//插入的编号是否存在,默认不存在
while(true){
if(temp.next==null){//已经在链表的尾部
break;
}
if(temp.next.num >personNode3.num){//找到位置,在temp后添加
break;
}
if(temp.next.num==personNode3.num){//编号已经存在,不能添加
flag=true;
break;
}
temp=temp.next;//后移,遍历
}
if(flag){
System.out.printf("添加的人员编号%d已经存在,不能添加\n",personNode3.num);
}else{
//添加数据到链表中,temp后的位置
personNode3.next=temp.next;
temp.next=personNode3;
}
//当退出while循环时,temp就指向了链表的最后
//将最后这个节点temp的next指向新的节点,新节点的pre指向最后个节点temp
temp.next=personNode3;
personNode3.pre=temp;
}
/**
* @Description: 修改双向链表的节点
* @Param:
* @Author: xz
* @return:
* @Date: 2020/7/22 21:34
*/
public void updateDoubleLinkedList(PersonNode3 personNode3){
if(head.next==null){
System.out.println("链表为空");
return;
}
//找到需要修改的节点,根据num修改
PersonNode3 temp=head.next;
boolean flag=false;//表示是否找到需要修改的节点编号,默认已找到
while(true){
if(temp==null){
break;//表示已遍历完链表
}
if(temp.num==personNode3.num){//已找到需要修改的节点编号
flag=true;
break;
}
temp=temp.next;
}
if(flag){//已找到需要修改的节点编号
temp.name=personNode3.name;
temp.aliasName=personNode3.aliasName;
}else{
System.out.printf("没有找到 编号%d的节点,不能修改\n",personNode3.num);
}
}
/**
* @Description: 删除双向链表中的一个节点
* @Param:
* @Author: xz
* @return:
* @Date: 2020/7/22 21:47
*/
public void deleteDoubleLinkedList(int num){
if(head.next==null){
System.out.println("链表为空,无法删除");
return;
}
PersonNode3 temp=head.next;//定义一个辅助节点,头节点的后一个节点,即待删除的节点
boolean flag=false;//标志是否找到待删除节点
while(true){
if (temp==null){//已找到链表最后节点的next域
break;
}
if(temp.num==num){//找到待删除的节点
flag=true;
break;
}
temp=temp.next;//临时变量后移
}
if(flag){//找到待删除的节点,可以删除
temp.pre.next=temp.next;
//如果temp是最后一个节点,temp.next=null,会报空指针,所以需要加一个判断
if(temp.next!=null){
temp.next.pre=temp.pre;
}
}else{//没有找到待删除的节点
System.out.printf("要删除的 d% 节点不存在",num);
}
}
/**
* @Description: 遍历双向链表
* @Param: []
* @Author: xz
* @return: void
* @Date: 2020/7/22 21:24
*/
public void selectDoubleLinkedList(){
//因为头节点不能动,所以需要一个临时变量来遍历
PersonNode3 temp=head.next;
//判断链表是否为空
if(temp==null){
System.out.println("双向链表为空");
return;
}
//遍历双向链表
while(true){
//判断是否到双向链表最后
if(temp == null){
break;
}
//输出节点信息
System.out.println(temp);
//将temp向后移动
temp = temp.next;
}
}
}
3、定义一个测试类
package com.rf.springboot01.dataStructure.doubleLinkedList2;
/**
* @description:
* @author: xiaozhi
* @create: 2020-07-22 22:18
*/
public class DoubleLinkedList3Test {
public static void main(String[] args) {
//创建双向链表节点
PersonNode3 p1 = new PersonNode3(1, "张三", "小张");
PersonNode3 p2 = new PersonNode3(2, "李四", "小李");
PersonNode3 p3 = new PersonNode3(3, "王五", "小王");
PersonNode3 p4 = new PersonNode3(4, "赵二", "小赵");
//添加双向链表节点
System.out.println("按编号顺序添加节点到双向链表==========");
DoubleLinkedList3 list = new DoubleLinkedList3();
list.insertDoubleLinkedList(p1);
list.insertDoubleLinkedList(p4);
list.insertDoubleLinkedList(p3);
list.insertDoubleLinkedList(p2);
//查询双向链表节点
list.selectDoubleLinkedList();
System.out.println("修改编号为1的原始双向链表的节点==========");
//创建一个双向链表节点,根据编号修改原始双向链表的节点
PersonNode3 p5 = new PersonNode3(1, "java", "学习java");
list.updateDoubleLinkedList(p5);
//查询双向链表节点
list.selectDoubleLinkedList();
System.out.println("删除编号为2的原始双向链表的节点==========");
list.deleteDoubleLinkedList(2);
//查询双向链表节点
list.selectDoubleLinkedList();
}
}
4、运行测试类,输出结果