哪位大神能够帮忙解决一下啊,求求了
4条回答 默认 最新
- CSDN专家-Fay 2021-06-04 11:29关注
使用Java模拟创建一个单项链表,数据域为Student类,题主是需要代码吗:
Manage类:
package com.ht.servlet; import java.sql.*; public class Manager { public static void main(String[] args) { Connection connection; String driver = "com.mysql.cj.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"; String user = "root"; String password = "123456"; try { Class.forName(driver); connection = DriverManager.getConnection(url,user,password); select(connection); } catch (Exception e) { e.printStackTrace(); } } public static void select(Connection con) throws SQLException { Statement statement = con.createStatement(); String sql = "select * from student"; ResultSet resultSet = statement.executeQuery(sql); int sid; String sname; int age; int grade; String hobby; //初始化链表 Link link= new Link(); while (resultSet.next()){ sid = resultSet.getInt("sid"); sname = resultSet.getString("sname"); age = resultSet.getInt("age"); grade = resultSet.getInt("grade"); hobby = resultSet.getString("hobby"); System.out.println("sid:"+sid+" "+"sname:"+sname +" "+"age:"+age+" "+"grade:"+grade+" "+"hobby:" +hobby ); //封装数据域Student Student stu = new Student(sid,sname,age,grade,hobby); link.addNode(stu);//添加节点 } link.display();//遍历所有节点 resultSet.close(); } }
Link链表类:
package com.ht.servlet; public class Link { Node head;// 头节点 public Link() { // 初始化头结点 head = new Node(); } /** * 添加节点(往链表后面添加节点) * * @param name */ public void addNode(Student stu) { Node curr = head;// 把头结点赋给当前节点 Node newNode = new Node(stu);// 构造新节点 while (true) { if (curr.getNext() == null) { curr.setNext(newNode);// 给当前节点设置下一个节点为新节点 break; } curr = curr.getNext();// 把下一个节点赋给当前节点继续循环 } } // 遍历链表 public void display() { Node curr = head.getNext();// 从头节点的下一个节点开始遍历 while (curr != null) { System.out.println(curr.getStudent().sname); curr = curr.getNext();// 把下一个节点赋给当前节点 } } }
Node节点类:
package com.ht.servlet; public class Node { Student student;//数据域 Node next;//链域 public Node(){} public Node(Student stu){ this.student=stu; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } }
Student数据域类:
package com.ht.servlet; public class Student { int sid; String sname; int age; int grade; String hobby; public Student(){} public Student(int sid, String sname, int age, int grade, String hobby) { super(); this.sid = sid; this.sname = sname; this.age = age; this.grade = grade; this.hobby = hobby; } @Override public String toString() { return "Student [sid=" + sid + ", sname=" + sname + ", age=" + age + ", grade=" + grade + ", hobby=" + hobby + "]"; } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥20 如何通过代码传输视频到亚马逊平台
- ¥15 php查询mysql数据库并显示至下拉列表中
- ¥15 freertos下使用外部中断失效
- ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
- ¥15 devserver配置完 启动服务 无法访问static上的资源
- ¥15 解决websocket跟c#客户端通信
- ¥30 Python调用dll文件输出Nan重置dll状态
- ¥15 浮动div的高度控制问题。
- ¥66 换电脑后应用程序报错
- ¥50 array数据同步问题