首先,我们需要定义一个学生类,包含学生的姓名和年龄:

class Student {
    String name;
    int age;
    Student next;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

接下来,我们创建一个链表,并添加7个学生:

public class Main {
    public static void main(String[] args) {
        // 创建链表的头结点
        Student head = new Student("student1", 18);
        // 添加7个学生
        Student student2 = new Student("student2", 19);
        Student student3 = new Student("student3", 20);
        Student student4 = new Student("student4", 21);
        Student student5 = new Student("student5", 22);
        Student student6 = new Student("student6", 23);
        Student student7 = new Student("student7", 24);

        head.next = student2;
        student2.next = student3;
        student3.next = student4;
        student4.next = student5;
        student5.next = student6;
        student6.next = student7;
    }
}

现在,我们可以开始完成各个步骤的操作:

  1. 假设班级有10个学生。里面存了7个学生。

    链表已经创建了7个学生的节点。

  2. 往index=2的位置插入一个学生。zhangsan,20

    首先,我们需要找到index=1的节点,然后将新学生节点插入到该节点的后面:

    Student newStudent = new Student("zhangsan", 20);
    Student current = head;
    
    for (int i = 1; i < 2; i++) {
        current = current.next;
    }
    
    newStudent.next = current.next;
    current.next = newStudent;
    
  3. 往index=7的位置插入一个学生。changfeng,18

    同样地,我们需要找到index=6的节点,然后将新学生节点插入到该节点的后面:

    Student newStudent2 = new Student("changfeng", 18);
    Student current2 = head;
    
    for (int i = 1; i < 7; i++) {
        current2 = current2.next;
    }
    
    newStudent2.next = current2.next;
    current2.next = newStudent2;
    
  4. 再删除index=4的学生的数据

    首先,我们需要找到index=3的节点,然后将其后面的节点删除:

    Student current3 = head;
    
    for (int i = 1; i < 4; i++) {
        current3 = current3.next;
    }
    
    current3.next = current3.next.next;
    
  5. 再删除index=7位置的学生

    同样地,我们需要找到index=6的节点,然后将其后面的节点删除:

    Student current4 = head;
    
    for (int i = 1; i < 6; i++) {
        current4 = current4.next;
    }
    
    current4.next = current4.next.next;
    
  6. 查找姓名是changfeng的节点。直接打印这个学生

    我们可以遍历整个链表,找到姓名为"changfeng"的节点,并打印出该学生的信息:

    Student current5 = head;
    
    while (current5 != null) {
        if (current5.name.equals("changfeng")) {
            System.out.println("姓名:" + current5.name + ",年龄:" + current5.age);
            break;
        }
        current5 = current5.next;
    }
    

最终的完整代码如下:

class Student {
    String name;
    int age;
    Student next;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

public class Main {
    public static void main(String[] args) {
        // 创建链表的头结点
        Student head = new Student("student1", 18);
        // 添加7个学生
        Student student2 = new Student("student2", 19);
        Student student3 = new Student("student3", 20);
        Student student4 = new Student("student4", 21);
        Student student5 = new Student("student5", 22);
        Student student6 = new Student("student6", 23);
        Student student7 = new Student("student7", 24);

        head.next = student2;
        student2.next = student3;
        student3.next = student4;
        student4.next = student5;
        student5.next = student6;
        student6.next = student7;

        // 往index=2的位置插入一个学生
        Student newStudent = new Student("zhangsan", 20);
        Student current = head;

        for (int i = 1; i < 2; i++) {
            current = current.next;
        }

        newStudent.next = current.next;
        current.next = newStudent;

        // 往index=7的位置插入一个学生
        Student newStudent2 = new Student("changfeng", 18);
        Student current2 = head;

        for (int i = 1; i < 7; i++) {
            current2 = current2.next;
        }

        newStudent2.next = current2.next;
        current2.next = newStudent2;

        // 删除index=4的学生的数据
        Student current3 = head;

        for (int i = 1; i < 4; i++) {
            current3 = current3.next;
        }

        current3.next = current3.next.next;

        // 删除index=7位置的学生
        Student current4 = head;

        for (int i = 1; i < 6; i++) {
            current4 = current4.next;
        }

        current4.next = current4.next.next;

        // 查找姓名是changfeng的节点,直接打印这个学生
        Student current5 = head;

        while (current5 != null) {
            if (current5.name.equals("changfeng")) {
                System.out.println("姓名:" + current5.name + ",年龄:" + current5.age);
                break;
            }
            current5 = current5.next;
        }
    }
}
``
使用链表分别完成以下事情 1假设班级有10个学生。 里面存了7个学生。 2往index=2的位置插入一个学生。 zhangsan20 3往index=7的位置插入一个学生。 changfeng18 4再删除 index=4的学生的数据 5再删除index=7位置的学生 6查找姓名是 changfeng 的节点。 直接打印这个学生

原文地址: https://www.cveoy.top/t/topic/ilNG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录