首先,我们需要定义一个学生类,包含学生的姓名和年龄:\n\njava\nclass Student {\n String name;\n int age;\n Student next;\n\n public Student(String name, int age) {\n this.name = name;\n this.age = age;\n }\n}\n\n\n接下来,我们创建一个链表,并添加7个学生:\n\njava\npublic class Main {\n public static void main(String[] args) {\n // 创建链表的头结点\n Student head = new Student("student1", 18);\n // 添加7个学生\n Student student2 = new Student("student2", 19);\n Student student3 = new Student("student3", 20);\n Student student4 = new Student("student4", 21);\n Student student5 = new Student("student5", 22);\n Student student6 = new Student("student6", 23);\n Student student7 = new Student("student7", 24);\n\n head.next = student2;\n student2.next = student3;\n student3.next = student4;\n student4.next = student5;\n student5.next = student6;\n student6.next = student7;\n }\n}\n\n\n现在,我们可以开始完成各个步骤的操作:\n\n1. 假设班级有10个学生。里面存了7个学生。\n\n 链表已经创建了7个学生的节点。\n\n2. 往index=2的位置插入一个学生。zhangsan,20\n\n 首先,我们需要找到index=1的节点,然后将新学生节点插入到该节点的后面:\n\n java\n Student newStudent = new Student("zhangsan", 20);\n Student current = head;\n\n for (int i = 1; i < 2; i++) {\n current = current.next;\n }\n\n newStudent.next = current.next;\n current.next = newStudent;\n \n\n3. 往index=7的位置插入一个学生。changfeng,18\n\n 同样地,我们需要找到index=6的节点,然后将新学生节点插入到该节点的后面:\n\n java\n Student newStudent2 = new Student("changfeng", 18);\n Student current2 = head;\n\n for (int i = 1; i < 7; i++) {\n current2 = current2.next;\n }\n\n newStudent2.next = current2.next;\n current2.next = newStudent2;\n \n\n4. 再删除index=4的学生的数据\n\n 首先,我们需要找到index=3的节点,然后将其后面的节点删除:\n\n java\n Student current3 = head;\n\n for (int i = 1; i < 4; i++) {\n current3 = current3.next;\n }\n\n current3.next = current3.next.next;\n \n\n5. 再删除index=7位置的学生\n\n 同样地,我们需要找到index=6的节点,然后将其后面的节点删除:\n\n java\n Student current4 = head;\n\n for (int i = 1; i < 6; i++) {\n current4 = current4.next;\n }\n\n current4.next = current4.next.next;\n \n\n6. 查找姓名是changfeng的节点。直接打印这个学生\n\n 我们可以遍历整个链表,找到姓名为"changfeng"的节点,并打印出该学生的信息:\n\n java\n Student current5 = head;\n\n while (current5 != null) {\n if (current5.name.equals("changfeng")) {\n System.out.println("姓名:" + current5.name + ",年龄:" + current5.age);\n break;\n }\n current5 = current5.next;\n }\n \n\n最终的完整代码如下:\n\njava\nclass Student {\n String name;\n int age;\n Student next;\n\n public Student(String name, int age) {\n this.name = name;\n this.age = age;\n }\n}\n\npublic class Main {\n public static void main(String[] args) {\n // 创建链表的头结点\n Student head = new Student("student1", 18);\n // 添加7个学生\n Student student2 = new Student("student2", 19);\n Student student3 = new Student("student3", 20);\n Student student4 = new Student("student4", 21);\n Student student5 = new Student("student5", 22);\n Student student6 = new Student("student6", 23);\n Student student7 = new Student("student7", 24);\n\n head.next = student2;\n student2.next = student3;\n student3.next = student4;\n student4.next = student5;\n student5.next = student6;\n student6.next = student7;\n\n // 往index=2的位置插入一个学生\n Student newStudent = new Student("zhangsan", 20);\n Student current = head;\n\n for (int i = 1; i < 2; i++) {\n current = current.next;\n }\n\n newStudent.next = current.next;\n current.next = newStudent;\n\n // 往index=7的位置插入一个学生\n Student newStudent2 = new Student("changfeng", 18);\n Student current2 = head;\n\n for (int i = 1; i < 7; i++) {\n current2 = current2.next;\n }\n\n newStudent2.next = current2.next;\n current2.next = newStudent2;\n\n // 删除index=4的学生的数据\n Student current3 = head;\n\n for (int i = 1; i < 4; i++) {\n current3 = current3.next;\n }\n\n current3.next = current3.next.next;\n\n // 删除index=7位置的学生\n Student current4 = head;\n\n for (int i = 1; i < 6; i++) {\n current4 = current4.next;\n }\n\n current4.next = current4.next.next;\n\n // 查找姓名是changfeng的节点,直接打印这个学生\n Student current5 = head;\n\n while (current5 != null) {\n if (current5.name.equals("changfeng")) {\n System.out.println("姓名:" + current5.name + ",年龄:" + current5.age);\n break;\n }\n current5 = current5.next;\n }\n }\n}\n


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

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