| public class Student { |
| |
| String name; |
| |
| String sex; |
| |
| int sno; |
| |
| int score; |
| |
| public String getName() { |
| return name; |
| } |
| |
| public void setName(String name) { |
| this.name = name; |
| } |
| |
| public String getSex() { |
| return sex; |
| } |
| |
| public void setSex(String sex) { |
| this.sex = sex; |
| } |
| |
| public int getSno() { |
| return sno; |
| } |
| |
| public void setSno(int sno) { |
| this.sno = sno; |
| } |
| |
| public int getScore() { |
| return score; |
| } |
| |
| public void setScore(int score) { |
| this.score = score; |
| } |
| |
| @Override |
| public String toString() { |
| return "Student{" + |
| "name='" + name + '\'' + |
| ", sex='" + sex + '\'' + |
| ", sno=" + sno + |
| ", score=" + score + |
| '}'; |
| } |
| |
| } |
| public class Node { |
| |
| Student stu= new Student(); |
| |
| Node next; |
| |
| } |
| public class MyArrarNode { |
| |
| Node head = new Node(); |
| |
| private int len=0; |
| |
| |
| public int size(){ |
| return len; |
| } |
| |
| |
| public void add(Student stu){ |
| |
| Node node = new Node(); |
| node.stu=stu; |
| |
| Node cur=head; |
| while (cur.next!=null){ |
| cur=cur.next; |
| } |
| cur.next=node; |
| |
| len++; |
| } |
| |
| |
| |
| public void show(){ |
| System.out.println("学号\t姓名\t性别\t成绩"); |
| Node cur=head; |
| for (int i = len; i > 0; i--) { |
| cur=cur.next; |
| System.out.println(" " + cur.stu.getSno() + " " + cur.stu.getName() |
| + " " + cur.stu.getSex() + " " + cur.stu.getScore()); |
| } |
| } |
| |
| |
| |
| |
| public void show1(){ |
| System.out.println("学号\t姓名\t性别\t成绩"); |
| Node cur=head; |
| while (cur.next!=null){ |
| cur=cur.next; |
| System.out.println(" " + cur.stu.getSno() + " " + cur.stu.getName() |
| + " " + cur.stu.getSex() + " " + cur.stu.getScore()); |
| } |
| } |
| |
| |
| |
| public void remove(int index){ |
| |
| Node cur=head; |
| for(int j=0; j<index; j++){ |
| cur=cur.next; |
| } |
| System.out.println("指定元素的前一个" + cur.stu); |
| |
| Node cur1 = cur.next; |
| System.out.println("指定元素" + cur1.stu); |
| |
| Node cur2 = cur1.next; |
| System.out.println("指定元素的后一个" + cur2.stu); |
| |
| cur.next=cur2; |
| |
| len--; |
| } |
| |
| |
| |
| public void removeSno(int sno){ |
| Node cur=head; |
| Node top; |
| while (cur.next!=null){ |
| top=cur; |
| cur=cur.next; |
| if(cur.stu.getSno()==sno){ |
| |
| top.next=cur.next; |
| |
| len--; |
| break; |
| } |
| } |
| } |
| |
| |
| public Student findBySno(int sno) { |
| Student stu = null; |
| Node cur=head; |
| while (cur.next!=null){ |
| cur=cur.next; |
| if(cur.stu.getSno()==sno){ |
| |
| stu=cur.stu; |
| break; |
| } |
| } |
| return stu; |
| } |
| |
| |
| public Student findByName(String name){ |
| Student stu = null; |
| Node cur=head; |
| while (cur.next!=null){ |
| cur=cur.next; |
| if(cur.stu.getName()==name){ |
| |
| stu=cur.stu; |
| break; |
| } |
| } |
| return stu; |
| } |
| |
| |
| public void sortAsc(){ |
| Node top; |
| Node cur; |
| Node nex; |
| for(int b = len-1; b>0; b--){ |
| int index=0; |
| for(int a = b; a>0; a--){ |
| cur=head; |
| top=cur; |
| for (int i = 0; i < index; i++) { |
| top=top.next; |
| } |
| for (int i = 0; i < index+1; i++) { |
| cur=cur.next; |
| } |
| nex=cur.next; |
| if(cur.stu.getScore()>nex.stu.getScore()){ |
| cur.next= nex.next; |
| top.next= nex; |
| nex.next=cur; |
| } |
| index++; |
| } |
| } |
| } |
| |
| |
| public void addSortAsc(Student stu){ |
| |
| Node node=new Node(); |
| node.stu=stu; |
| |
| Node top; |
| Node cur; |
| for (int i = 0; i < len; i++) { |
| |
| cur=head; |
| top=cur; |
| for (int j = 0; j < i; j++) { |
| top=top.next; |
| } |
| for (int j = 0; j < i+1; j++) { |
| cur=cur.next; |
| } |
| if (stu.getScore()<cur.stu.getScore()){ |
| |
| top.next=node; |
| node.next=cur; |
| |
| len++; |
| |
| break; |
| }else { |
| |
| continue; |
| } |
| } |
| Node end=head; |
| for(int j=0; j<len; j++){ |
| end=end.next; |
| } |
| |
| if(stu.getScore() >= end.stu.getScore()){ |
| end.next=node; |
| len++; |
| } |
| } |
| |
| |
| public void sortDesc(){ |
| Node top; |
| Node cur; |
| Node nex; |
| for(int b = len-1; b>0; b--){ |
| int index=0; |
| for(int a = b; a>0; a--){ |
| cur=head; |
| top=cur; |
| for (int i = 0; i < index; i++) { |
| top=top.next; |
| } |
| for (int i = 0; i < index+1; i++) { |
| cur=cur.next; |
| } |
| nex=cur.next; |
| if(cur.stu.getScore() < nex.stu.getScore()){ |
| cur.next= nex.next; |
| top.next= nex; |
| nex.next=cur; |
| } |
| index++; |
| } |
| } |
| } |
| |
| |
| public void addSortDesc(Student stu){ |
| |
| Node node=new Node(); |
| node.stu=stu; |
| |
| Node top; |
| Node cur; |
| for (int i = 0; i < len; i++) { |
| |
| cur=head; |
| top=cur; |
| for (int j = 0; j < i; j++) { |
| top=top.next; |
| } |
| for (int j = 0; j < i+1; j++) { |
| cur=cur.next; |
| } |
| if (stu.getScore()>cur.stu.getScore()){ |
| |
| top.next=node; |
| node.next=cur; |
| |
| len++; |
| |
| break; |
| }else { |
| |
| continue; |
| } |
| } |
| Node end=head; |
| for(int j=0; j<len; j++){ |
| end=end.next; |
| } |
| |
| if(stu.getScore() <= end.stu.getScore()){ |
| end.next=node; |
| len++; |
| } |
| } |
| |
| } |
| public class test { |
| public static void main(String[] args) { |
| MyArrarNode arrarNode=new MyArrarNode(); |
| |
| |
| Student student=new Student(); |
| student.setName("张三"); |
| student.setSex("男"); |
| student.setSno(3); |
| student.setScore(68); |
| arrarNode.add(student); |
| Student student1=new Student(); |
| student1.setName("张四"); |
| student1.setSex("女"); |
| student1.setSno(9); |
| student1.setScore(95); |
| arrarNode.add(student1); |
| Student student2=new Student(); |
| student2.setName("李三"); |
| student2.setSex("男"); |
| student2.setSno(6); |
| student2.setScore(72); |
| arrarNode.add(student2); |
| |
| System.out.println("当前长度为:" + arrarNode.size()); |
| |
| |
| arrarNode.show1(); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| System.out.println(arrarNode.findBySno(6)); |
| System.out.println(arrarNode.findByName("李三")); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| System.out.println("--- 成绩降序 ---"); |
| arrarNode.sortDesc(); |
| arrarNode.show1(); |
| System.out.println("--- 插入一条数据 ---"); |
| Student student3=new Student(); |
| student3.setName("李五"); |
| student3.setSex("男"); |
| student3.setSno(7); |
| student3.setScore(68); |
| arrarNode.addSortDesc(student3); |
| arrarNode.show1(); |
| |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2022-09-29 马踏棋盘算法
2022-09-29 弗洛伊德算法
2022-09-29 迪杰斯特拉算法