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(" "); } 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,201622118,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,201622111,null); Student stu2 =new Student("Tom",98,201622112,stu1); Student stu3 =new Student("Jack",100,201622113,stu2); Student head = stu3; Student stu4 =new Student("Jony",88,201622114,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,201622115); 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);; } } } }
上一篇:
IDEA上Java项目控制台中文乱码