使用链表操作实体类

发布时间 2023-09-29 18:53:46作者: DogLeftover
  • 编写实体类
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();

    }
}