首页 > 其他分享 >使用链表操作实体类

使用链表操作实体类

时间:2023-09-29 18:55:13浏览次数:28  
标签:Node 实体类 cur int next 链表 stu 操作 public

  • 编写实体类
public class Student {

    String name; // 姓名

    String sex; // 性别

    int sno; // 学号

    int score; // 成绩

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getSno() {
        return sno;
    }

    public void setSno(int sno) {
        this.sno = sno;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", sno=" + sno +
                ", score=" + score +
                '}';
    }

}
  • 创建节点类
public class Node {

    Student stu= new Student();

    Node next;

}
  • 编写方法
public class MyArrarNode {

    Node head = new Node();

    private int len=0;

    // 获取长度
    public int size(){
        return len;
    }

    //添加一个学生,放到数组的最后
    public void add(Student stu){
        // 新建节点
        Node node = new Node();
        node.stu=stu;
        // 如果头节点后没有元素,则直接拼接,如果有,则找到最后的元素
        Node cur=head;
        while (cur.next!=null){
            cur=cur.next;
        }
        cur.next=node;
        // 长度
        len++;
    }

    //显示所有学生的信息
    //[head][][][]
    public void show(){
        System.out.println("学号\t姓名\t性别\t成绩");
        Node cur=head;
        for (int i = len; i > 0; i--) {
            cur=cur.next;
            System.out.println(" " + cur.stu.getSno() + "  " + cur.stu.getName()
                    + "  " + cur.stu.getSex() + "  " + cur.stu.getScore());
        }
    }

    //显示所有学生的信息
    //[head][][][]
    // 方式2
    public void show1(){
        System.out.println("学号\t姓名\t性别\t成绩");
        Node cur=head;
        while (cur.next!=null){
            cur=cur.next;
            System.out.println(" " + cur.stu.getSno() + "  " + cur.stu.getName()
                    + "  " + cur.stu.getSex() + "  " + cur.stu.getScore());
        }
    }

    //删除指定位置的数据
    //[head][][][]
    public void remove(int index){
        // 找到指定元素的前一个
        Node cur=head;
        for(int j=0; j<index; j++){
            cur=cur.next;
        }
        System.out.println("指定元素的前一个" + cur.stu);
        // 找到指定的元素
        Node cur1 = cur.next;
        System.out.println("指定元素" + cur1.stu);
        // 指定元素的后一个
        Node cur2 = cur1.next;
        System.out.println("指定元素的后一个" + cur2.stu);
        // 前一个元素直接拼接后一个元素
        cur.next=cur2;
        // 长度--
        len--;
    }

    //根据学号删除
    // [head][3][9][6][]
    public void removeSno(int sno){
        Node cur=head;
        Node top; // 指向当前节点的上一个节点
        while (cur.next!=null){
            top=cur; // 指向下一个节点前,先存入top
            cur=cur.next;  // head=head.next
            if(cur.stu.getSno()==sno){
                // 找到后删除,前一个元素直接拼接后一个元素
                top.next=cur.next;
                //
                len--;
                break;
            }
        }
    }

    //根据学号查找学生
    public Student findBySno(int sno) {
        Student stu = null;
        Node cur=head;
        while (cur.next!=null){
            cur=cur.next;  // head=head.next
            if(cur.stu.getSno()==sno){
                // 找到后,跳出
                stu=cur.stu;
                break;
            }
        }
        return stu;
    }

    //根据姓名查找学生
    public Student findByName(String name){
        Student stu = null;
        Node cur=head;
        while (cur.next!=null){
            cur=cur.next;  // head=head.next
            if(cur.stu.getName()==name){
                // 找到后,跳出
                stu=cur.stu;
                break;
            }
        }
        return stu;
    }

    //成绩升序排序
    public void sortAsc(){
        Node top;
        Node cur;
        Node nex;
        for(int b = len-1; b>0; b--){  // 3轮比较
            int index=0;
            for(int a = b; a>0; a--){
                cur=head;
                top=cur;
                for (int i = 0; i < index; i++) {  // 找top节点
                    top=top.next;
                }
                for (int i = 0; i < index+1; i++) {  // 找cur节点
                    cur=cur.next;
                }
                nex=cur.next;
                if(cur.stu.getScore()>nex.stu.getScore()){
                    cur.next= nex.next;
                    top.next= nex;
                    nex.next=cur;
                }
                index++;
            }
        }
    }

    //假如已经按照成绩升序排序之后,将学生添加到对应的位置,保证数组仍然是升序
    public void addSortAsc(Student stu){
        // 新建元素节点
        Node node=new Node();
        node.stu=stu;

        Node top;
        Node cur;
        for (int i = 0; i < len; i++) {    // 比较len轮
            // 每次找到要比较的节点和指定节点的前一个节点
            cur=head;
            top=cur;
            for (int j = 0; j < i; j++) {  // 找top节点
                top=top.next;
            }
            for (int j = 0; j < i+1; j++) {  // 找cur节点
                cur=cur.next;
            }
            if (stu.getScore()<cur.stu.getScore()){  // 在某本节点前时
                // 插入位置
                top.next=node;
                node.next=cur;
                // 长度++
                len++;
                // 结束整个循环
                break;
            }else { // 大于等于node1
                // 不成立则continue,进入下一轮比较
                continue;
            }
        }
        Node end=head;
        for(int j=0; j<len; j++){  // 找到最后
            end=end.next;
        }
        // 当插入的值比数组中最大的值还大时,插入到最后
        if(stu.getScore() >= end.stu.getScore()){
            end.next=node;
            len++;
        }
    }

    //成绩降序排序
    public void sortDesc(){
        Node top;
        Node cur;
        Node nex;
        for(int b = len-1; b>0; b--){  // 3轮比较
            int index=0;
            for(int a = b; a>0; a--){
                cur=head;
                top=cur;
                for (int i = 0; i < index; i++) {  // 找top节点
                    top=top.next;
                }
                for (int i = 0; i < index+1; i++) {  // 找cur节点
                    cur=cur.next;
                }
                nex=cur.next;
                if(cur.stu.getScore() < nex.stu.getScore()){
                    cur.next= nex.next;
                    top.next= nex;
                    nex.next=cur;
                }
                index++;
            }
        }
    }

    //假如已经按照成绩降序排序之后,将学生添加到对应的位置,保证数组仍然是降序序
    public void addSortDesc(Student stu){
        // 新建元素节点
        Node node=new Node();
        node.stu=stu;

        Node top;
        Node cur;
        for (int i = 0; i < len; i++) {    // 比较len轮
            // 每次找到要比较的节点和指定节点的前一个节点
            cur=head;
            top=cur;
            for (int j = 0; j < i; j++) {  // 找top节点
                top=top.next;
            }
            for (int j = 0; j < i+1; j++) {  // 找cur节点
                cur=cur.next;
            }
            if (stu.getScore()>cur.stu.getScore()){  // 在某本节点前时
                // 插入位置
                top.next=node;
                node.next=cur;
                // 长度++
                len++;
                // 结束整个循环
                break;
            }else { // 小于等于node1
                // 不成立则continue,进入下一轮比较
                continue;
            }
        }
        Node end=head;
        for(int j=0; j<len; j++){  // 找到最后
            end=end.next;
        }
        // 当插入的值比数组中最大的值还大时,插入到最后
        if(stu.getScore() <= end.stu.getScore()){
            end.next=node;
            len++;
        }
    }

}
  • 测试
public class test {
    public static void main(String[] args) {
        MyArrarNode arrarNode=new MyArrarNode();

        // 添加数据
        Student student=new Student();
        student.setName("张三");
        student.setSex("男");
        student.setSno(3);
        student.setScore(68);
        arrarNode.add(student);
        Student student1=new Student();
        student1.setName("张四");
        student1.setSex("女");
        student1.setSno(9);
        student1.setScore(95);
        arrarNode.add(student1);
        Student student2=new Student();
        student2.setName("李三");
        student2.setSex("男");
        student2.setSno(6);
        student2.setScore(72);
        arrarNode.add(student2);
        // len
        System.out.println("当前长度为:" + arrarNode.size());
        // 显示
//        arrarNode.show();
        arrarNode.show1();

        // 删除指定位置的元素
//        arrarNode.remove(1);
//        arrarNode.show1();

        // 根据学号删除
//        arrarNode.removeSno(6);
//        arrarNode.show1();

        // 查找
        System.out.println(arrarNode.findBySno(6));
        System.out.println(arrarNode.findByName("李三"));

        // 排序
//        System.out.println("--- 成绩升序 ---");
//        arrarNode.sortAsc();
//        arrarNode.show1();
//        System.out.println("--- 插入一条数据 ---");
//        Student student3=new Student();
//        student3.setName("李五");
//        student3.setSex("男");
//        student3.setSno(7);
//        student3.setScore(95);
//        arrarNode.addSortAsc(student3);
//        arrarNode.show1();

        System.out.println("--- 成绩降序 ---");
        arrarNode.sortDesc();
        arrarNode.show1();
        System.out.println("--- 插入一条数据 ---");
        Student student3=new Student();
        student3.setName("李五");
        student3.setSex("男");
        student3.setSno(7);
        student3.setScore(68);
        arrarNode.addSortDesc(student3);
        arrarNode.show1();

    }
}

标签:Node,实体类,cur,int,next,链表,stu,操作,public
From: https://www.cnblogs.com/dogleftover/p/17737172.html

相关文章

  • 使用数组操作实体类
    编写实体类publicclassStudent{Stringname;//姓名Stringsex;//性别intsno;//学号intscore;//成绩publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;......
  • git日常操作汇总
    1、如果本地已经有代码,现在想用git管理,操作步骤如下:1、进入项目根目录下,执行gitinit2、添加所有文件gitadd.3、提交gitcommit-m'init'4、关联到远程仓库,git上先创建一个项目,然后再关联gitremoteaddoriginhttps://gitee.com/xxx/hr.git5、gitpush-uorigin......
  • TCP/IP连接数的最大值取决于操作系统、硬件和应用程序等多个因素
    TCP/IP连接数的最大值取决于操作系统、硬件和应用程序等多个因素。下面是一些常见操作系统中TCP/IP连接数的默认值和最大值:Windows10/WindowsServer2019:默认值为16384,最大值为16777216Windows8/WindowsServer2012:默认值为16384,最大值为16777216Windows7/WindowsServer......
  • 使用链表模拟队列和栈
    使用链表模拟队列案例1//创建节点类publicclassNode{intn;Nodenext;}//编写方法publicclassQueue{Nodehead=newNode();Nodelast=newNode();privateintlen=0;publicintsize(){returnthis.len;}......
  • 模拟链表
    创建节点类publicclassNode{intn;Nodenext;}编写方法publicclassMyLinkList{Nodehead=newNode();privateintlen=0;//获取长度publicintsize(){returnlen;}//添加元素到最后publicvoid......
  • 链表插入排序
    创建节点类publicclassNode{intn;Nodenext;}第1次推导publicclasstest{publicstaticvoidmain(String[]args){//新建节点Nodenode1=newNode();node1.n=2;Nodenode2=newNode();node......
  • 链表冒泡排序
    创建节点类publicclassNode{intn;Nodenext;}第1次推导publicclasstest{publicstaticvoidmain(String[]args){//新建节点Nodenode1=newNode();node1.n=9;Nodenode2=newNode();no......
  • [笔记]操作系统_2024年考纲
    一、操作系统基础(一)操作系统的基本概念(二)操作系统发展历程(三)程序运行环境1.CPU运行模式内核模式,用户模式。2.中断和异常的处理3.系统调用4.程序的链接与装入5.程序运行时的内存映像与地址空间(四)操作系统结构分层,模块化,宏内核,微内核,外核。(五)操作系统引导(六)虚拟......
  • asp .net core Exceptionless日志操作
    exceptionless官网使用说明安装Nuget包添加引用usingExceptionless;usingExceptionless.Logging;初始化-秘钥-请求地址//构造函数初始化日志参数(日志系统地址,项目模块)publicDefaultController(){if(string.IsNullOrWhiteSpace(ExceptionlessClient.Default.Config......
  • sql查询之拼接外表或该表不存在的数据,简化多表联查的操作
    (内容)1.引言最近写项目时,用到了多表联查的知识点,我需要传article类和web_user类的username的参数这是我的三个表--MySQLdump10.13Distrib8.0.26,forWin64(x86_64)----Host:127.0.0.1Database:web-----------------------------------------------------......