学习记录 --- java实现单链表(简单的学生信息的增删改)

1. 创建节点

节点里的数据一般分为数据节点、和指向下一个节点的变量(指针)。

class StuNode{
    public int no;
    public String name;
    public StuNode next;

    public StuNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public StuNode(){

    }

    @Override
    public String toString() {
        return "StuNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}
2.创建单链表

创建一个单链表类、里面设置一个头结点、再写一些基本的增删改节点方法。

//单链表
class SingleList{
    private StuNode head = new StuNode(0,"");

    //插入节点
    public void addNode(StuNode node){
        StuNode item = head;
        //找到最后一个节点、使用尾插法
        while (item.next != null){
            item = item.next;
        }
        item.next = node;
    }

    //按顺序插入
    public void addByOrder(StuNode node){
        StuNode item = head;
        StuNode item1 = item.next;
        while (item1 != null){

            if (item1.no > node.no){
                //表示已经找到
                break;
            }else if (item.no == node.no){
                System.out.println("节点编号不能重复!");
                return;
            }
            item = item.next;
            item1 = item.next;
        }
        node.next = item.next;
        item.next = node;
    }

    //根据no值更新链表信息
    public void updateNode(StuNode newnode){
        StuNode item = head.next;
        if (item == null){
            System.out.println("该链表为空!");
            return;
        }
        while (item != null){
            if (item.no == newnode.no){
                item.name = newnode.name;
                return;
            }
            item = item.next;
        }
        System.out.printf("没有找到学号为%d的学生。\n",newnode.no);
    }
    //根据学号no删除节点
    public void delNode(int no){
        StuNode item0 = head;
        StuNode item = head.next;
        if (item == null){
            //如果链表为空
            System.out.println("该链表为空!");
            return;
        }
        //遍历查找学号为no的节点
        while (item != null){
            //找到节点
            if (item.no == no){
                item0.next = item.next;
                return;
            }
            item0 = item0.next;
            item = item0.next;
        }
        System.out.printf("没有找到学号为%d的学生节点。\n",no);
    }
    //打印显示链表
    public void showList(){
        StuNode item = head.next;
        if (item == null){
            System.out.println("该链表为空!");
            return;
        }
        //遍历输出
        while (item != null){
            System.out.println(item);
            item = item.next;
        }
    }

}
3.测试

测试就在main方法里进行。分别添加三个数据、然后修改学号1001的名字为新张三、最后删除这个节点。分别打印查看结果。

public static void main(String[] args) {
        //测试
        StuNode s1 = new StuNode(1001,"张三");
        StuNode s2 = new StuNode(1002,"李四");
        StuNode s3 = new StuNode(1003,"王五");
        //创建一个单链表
        SingleList sl = new SingleList();

        sl.addByOrder(s3);
        sl.addByOrder(s2);
        sl.addByOrder(s1);
        System.out.println("------------顺序添加。addByOrder------------");
        sl.showList();
        System.out.println("-----------修改1001。updateNode-------------");
        sl.updateNode(new StuNode(1001,"新张三"));
        sl.showList();
        System.out.println("-----------删除1001。delNode-------------");
        sl.delNode(1001);
        sl.showList();

    }

测试结果:

在这里插入图片描述

整体代码
package com.yzx.singlelist单链表;

/**
 * 单链表:以存储学生信息为例
 *
 */
public class SingleListDemo {
    public static void main(String[] args) {
        //测试
        StuNode s1 = new StuNode(1001,"张三");
        StuNode s2 = new StuNode(1002,"李四");
        StuNode s3 = new StuNode(1003,"王五");
        //创建一个单链表
        SingleList sl = new SingleList();

        sl.addByOrder(s3);
        sl.addByOrder(s2);
        sl.addByOrder(s1);
        System.out.println("------------顺序添加。addByOrder------------");
        sl.showList();
        System.out.println("-----------修改1001。updateNode-------------");
        sl.updateNode(new StuNode(1001,"新张三"));
        sl.showList();
        System.out.println("-----------删除1001。delNode-------------");
        sl.delNode(1001);
        sl.showList();

    }
}

//单链表
class SingleList{
    private StuNode head = new StuNode(0,"");

    //插入节点
    public void addNode(StuNode node){
        StuNode item = head;
        //找到最后一个节点、使用尾插法
        while (item.next != null){
            item = item.next;
        }
        item.next = node;
    }

    //按顺序插入
    public void addByOrder(StuNode node){
        StuNode item = head;
        StuNode item1 = item.next;
        while (item1 != null){

            if (item1.no > node.no){
                //表示已经找到
                break;
            }else if (item.no == node.no){
                System.out.println("节点编号不能重复!");
                return;
            }
            item = item.next;
            item1 = item.next;
        }
        node.next = item.next;
        item.next = node;
    }

    //根据no值更新链表信息
    public void updateNode(StuNode newnode){
        StuNode item = head.next;
        if (item == null){
            System.out.println("该链表为空!");
            return;
        }
        while (item != null){
            if (item.no == newnode.no){
                item.name = newnode.name;
                return;
            }
            item = item.next;
        }
        System.out.printf("没有找到学号为%d的学生。\n",newnode.no);
    }
    //根据学号no删除节点
    public void delNode(int no){
        StuNode item0 = head;
        StuNode item = head.next;
        if (item == null){
            //如果链表为空
            System.out.println("该链表为空!");
            return;
        }
        //遍历查找学号为no的节点
        while (item != null){
            //找到节点
            if (item.no == no){
                item0.next = item.next;
                return;
            }
            item0 = item0.next;
            item = item0.next;
        }
        System.out.printf("没有找到学号为%d的学生节点。\n",no);
    }
    //打印显示链表
    public void showList(){
        StuNode item = head.next;
        if (item == null){
            System.out.println("该链表为空!");
            return;
        }
        //遍历输出
        while (item != null){
            System.out.println(item);
            item = item.next;
        }
    }

}

//节点、存储学生信息
class StuNode{
    public int no;
    public String name;
    public StuNode next;

    public StuNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public StuNode(){

    }

    @Override
    public String toString() {
        return "StuNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}
  • 3
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值