100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > java单链表实现小学生管理系统

java单链表实现小学生管理系统

时间:2019-12-27 11:48:04

相关推荐

java单链表实现小学生管理系统

题目:用单链表实现小学生信息(姓名、学号、成绩等)的增删改查与排序

最开始写的时候不知道java中有linklist类,所以用嵌套的方法做的

//Student.java

import java.util.*;public class Student {private int score;private double ID;private String name;private Student next;public Student(String n,int s,double i,Student stu) {name = n;score = s;ID = i;next = stu;}public static void changeStudent(Student stu,String n,int s,double i) {stu.name = n;stu.score = s;stu.ID = i;}public void output() {System.out.println("Student name:"+name);System.out.println("Student ID:"+ID);System.out.println("Student score:"+score);System.out.print("\n");}public int getScore() {return score;}public double getID() {return ID;}public String getName() {return name;}public static void addStudent(Student stu,Student head) {Student temp = head;while(temp.next != null) {temp = temp.next;}temp.next = stu;}public static void deleteStudent(Student stu,Student head) {Student temp = head;if(stu.next == null) {while(temp.next.next != null) {temp = temp.next;}temp.next = null;}else if(stu==head) {head = stu.next;stu.next=null;}else {temp = head;while(head.next != stu) {temp = temp.next; }temp.next = stu.next;stu.next = null;}}public static void searchStudent(Student head) {Scanner in = new Scanner(System.in);System.out.print("please input student ID:");double temp = in.nextDouble();while(head !=null ) {if(head.ID==temp) {head.output();break;}head = head.next;}}public static void sortScore(Student head) {Student temp1,temp2,temp3;temp3=new Student("aaa",33,22118,null);for(temp1=head;temp1.next!=null;temp1 = temp1.next) {for(temp2=head;temp2.next!=null;temp2=temp2.next) {if(temp2.getScore()>temp2.next.getScore()) {temp3.changeStudent(temp3,temp2.next.getName(), temp2.next.getScore(), temp2.next.getID());temp2.next.changeStudent(temp2.next, temp2.getName(), temp2.getScore(), temp2.getID());temp2.changeStudent(temp2, temp3.getName(), temp3.getScore(), temp3.getID());}}}}public static void outStudent(Student head) {Student temp = head;while(temp != null) {temp.output();temp = temp.next;}}}

//StudentTest.java

import java.util.*;public class StudentTest {public static void main(String[] args) {Student stu1 =new Student("Bob",99,22111,null);Student stu2 =new Student("Tom",98,22112,stu1);Student stu3 =new Student("Jack",100,22113,stu2);Student head = stu3;Student stu4 =new Student("Jony",88,22114,null);while(1==1) {Scanner in = new Scanner(System.in);System.out.println("Please input your choice:");System.out.println("增:1修改:2删除:3查找:4排序:5 展示:6 退出:0");int choice = in.nextInt();switch(choice) {case 1:Student.addStudent(stu4,head);Student.outStudent(head);break;case 3:Student.deleteStudent(stu4, head);Student.outStudent(head);break;case 2:Student.changeStudent(stu3,"Tom",67,22115);Student.outStudent(head);break;case 4:Student.searchStudent(head);break;case 5:Student.sortScore(head);Student.outStudent(head);break;case 6:Student.outStudent(head);break;case 0:System.exit(0);;}}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。