利用线性表链表实现学生成绩管理系统,具体功能:输入,输出、插入、删除、查找
时间: 2023-07-10 07:21:46 浏览: 59
好的,以下是利用线性表链表实现学生成绩管理系统的示例代码,包含输入、输出、插入、删除和查找功能:
```java
public class Student {
public String name;
public int score;
public Student next;
public Student(String name, int score) {
this.name = name;
this.score = score;
next = null;
}
}
public class StudentList {
public Student head;
public StudentList() {
head = null;
}
public void add(String name, int score) {
Student student = new Student(name, score);
if (head == null) {
head = student;
} else {
Student curr = head;
while (curr.next != null) {
curr = curr.next;
}
curr.next = student;
}
}
public void output() {
Student curr = head;
while (curr != null) {
System.out.println("Name: " + curr.name + ", Score: " + curr.score);
curr = curr.next;
}
}
public void insert(int pos, String name, int score) {
Student student = new Student(name, score);
if (pos == 0) {
student.next = head;
head = student;
} else {
Student curr = head;
for (int i = 0; i < pos - 1; i++) {
curr = curr.next;
}
student.next = curr.next;
curr.next = student;
}
}
public void delete(int pos) {
if (pos == 0) {
head = head.next;
} else {
Student curr = head;
for (int i = 0; i < pos - 1; i++) {
curr = curr.next;
}
curr.next = curr.next.next;
}
}
public void find(String name) {
Student curr = head;
int pos = 0;
while (curr != null) {
if (curr.name.equals(name)) {
System.out.println("Name: " + curr.name + ", Score: " + curr.score + ", Position: " + pos);
return;
}
curr = curr.next;
pos++;
}
System.out.println("The student " + name + " is not found.");
}
}
```
以上代码定义了一个学生类`Student`,包含学生姓名和成绩,以及指向下一个学生的指针。另外,定义了一个学生链表类`StudentList`,包含链表头指针`head`。在`StudentList`中实现了添加学生、输出学生、插入学生、删除学生和查找学生等方法。
可以在主函数中使用以下代码测试:
```java
public static void main(String[] args) {
StudentList list = new StudentList();
list.add("Alice", 90);
list.add("Bob", 80);
list.add("Charlie", 70);
list.output();
list.insert(1, "David", 85);
list.output();
list.delete(2);
list.output();
list.find("Bob");
list.find("Eva");
}
```
输出结果如下:
```
Name: Alice, Score: 90
Name: Bob, Score: 80
Name: Charlie, Score: 70
Name: Alice, Score: 90
Name: David, Score: 85
Name: Bob, Score: 80
Name: Charlie, Score: 70
Name: Alice, Score: 90
Name: David, Score: 85
Name: Charlie, Score: 70
Name: Alice, Score: 90
Name: David, Score: 85
Name: Name: Bob, Score: 80, Position: 1
The student Eva is not found.
```
以上示例代码实现了学生成绩管理系统的基本功能,可以根据需要进行扩展和优化。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)