Data Structures-05:单链表(节点的增删改查)

文章目录

    • 1. 单链表添加节点(直接添加到末尾),查询节点信息
    • 2. 单链表添加节点(添加到指定位置)
    • 3. 单链表节点的修改
    • 4. 单链表节点的删除

Data Structures-05:单链表(节点的增删改查)_第1张图片
节点实体类:

class HeroNode{
    public int no;
    public String name;
    public String nickName;
    public HeroNode next;//指向下一个节点

    //构造器
    public HeroNode(int no,String name,String nickName){
        this.no = no;
        this.nickName = nickName;
        this.name = name;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}

1. 单链表添加节点(直接添加到末尾),查询节点信息

Data Structures-05:单链表(节点的增删改查)_第2张图片
Data Structures-05:单链表(节点的增删改查)_第3张图片
在链表结尾添加节点,并查看链表节点

class SingleLinkedList{
    //创建一个头节点
    private HeroNode head = new HeroNode(0,"","");

    //添加节点到单向链表
    public void add(HeroNode heroNode){
        //定义一个辅助变量指向头结点
        HeroNode temp = head;
        while (true){
            //如果找到了节点的最后就跳出循环
            if(temp.next == null){
                break;
            }
            //如果没有找到节点最后,就让temp后移
            temp = temp.next;
        }
        //当退出while循环时,temp就指向了链表的最后
        //将最后这个节点的next指向新的节点
        temp.next = heroNode;
    }
    
     //显示链表
    public void show(){
        if(head.next == null){
            System.out.println("链表为空");
            return;
        }
        HeroNode temp = head.next;
        while (true){
            if(temp==null){
                break;
            }
            //输出节点信息
            System.out.println(temp);
            temp = temp.next;
        }
    }
}

测试类:

public class SingleLinkedListDemo {
    public static void main(String[] args) {
        //先创建几个节点
        HeroNode heroNode1 = new HeroNode(1,"宋江","及时雨");
        HeroNode heroNode2 = new HeroNode(2,"卢俊义","玉麒麟");
        HeroNode heroNode3 = new HeroNode(3,"吴用","智多星");
        HeroNode heroNode4 = new HeroNode(4,"林冲","豹子头");
        //创建链表
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        //加入节点
        singleLinkedList.add(heroNode1);
        singleLinkedList.add(heroNode2);
        singleLinkedList.add(heroNode3);
        singleLinkedList.add(heroNode4);
        //显示链表
        singleLinkedList.show();
    }
}

2. 单链表添加节点(添加到指定位置)

Data Structures-05:单链表(节点的增删改查)_第4张图片
在链表的指定位置添加节点:

public void addByOrder(HeroNode heroNode){
    HeroNode temp = head;
    boolean flag = false;//添加的编号是否存在
    while (true){
        if(temp.next == null){//到达链表的最后
            break;
        }
        if(temp.next.no>heroNode.no){//找到,在temp的后面插入
            break;
        }else if(temp.next.no == heroNode.no){
            flag = true;
            break;
        }
        temp = temp.next;
    }

    //判断flag的值,如果flag为true不能添加,如果为false,可以添加
    if(flag){
        System.out.printf("准备插入的英雄的编号已经存在\n",heroNode.no);
    }else{
        heroNode.next = temp.next;
        temp.next = heroNode;
    }
}

测试类:

public class SingleLinkedListDemo {
    public static void main(String[] args) {
        //先创建几个节点
        HeroNode heroNode1 = new HeroNode(1,"宋江","及时雨");
        HeroNode heroNode2 = new HeroNode(2,"卢俊义","玉麒麟");
        HeroNode heroNode3 = new HeroNode(3,"吴用","智多星");
        HeroNode heroNode4 = new HeroNode(4,"林冲","豹子头");
        //创建链表
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        //按照编号加入
        singleLinkedList.addByOrder(heroNode1);
        singleLinkedList.addByOrder(heroNode4);
        singleLinkedList.addByOrder(heroNode3);
        singleLinkedList.addByOrder(heroNode2);
        //显示链表
        singleLinkedList.show();
    }
}

Data Structures-05:单链表(节点的增删改查)_第5张图片

3. 单链表节点的修改

public void update(HeroNode newHeroNode){
   if(head.next == null){
       System.out.println("链表为空");
       return;
   }

   //根据编号no修改节点的信息(不包括头结点)
   HeroNode 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.println("没有找到修改的节点");
   }
}

测试类:

//先创建几个节点
HeroNode heroNode1 = new HeroNode(1,"宋江","及时雨");
HeroNode heroNode2 = new HeroNode(2,"卢俊义","玉麒麟");
HeroNode heroNode3 = new HeroNode(3,"吴用","智多星");
HeroNode heroNode4 = new HeroNode(4,"林冲","豹子头");
//创建链表
SingleLinkedList singleLinkedList = new SingleLinkedList();
//按照编号加入
singleLinkedList.addByOrder(heroNode1);
singleLinkedList.addByOrder(heroNode4);
singleLinkedList.addByOrder(heroNode3);
singleLinkedList.addByOrder(heroNode2);

HeroNode newHeroNode = new HeroNode(2,"小卢","玉麒麟~~");
singleLinkedList.update(newHeroNode);
//显示链表
singleLinkedList.show();

在这里插入图片描述

4. 单链表节点的删除

Data Structures-05:单链表(节点的增删改查)_第6张图片

public void delete(int no){
    HeroNode temp = head;
    boolean flag = false;
    
    while (true){
        if(temp.next==null){
            break;
        }
        if(temp.next.no==no){
            flag = true;
            break;
        }
        temp = temp.next;
    }
    
    if(flag){
        temp.next = temp.next.next;
    }else{
        System.out.printf("没有找到相关编号的节点",no);
    }
}

你可能感兴趣的:(数据结构与算法)