TygZ3 2021-06-04 11:25 采纳率: 100%
浏览 50
已采纳

Java实训内容求助

哪位大神能够帮忙解决一下啊,求求了

  • 写回答

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 + "]";
    	}
    	
         
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 有没有人知道这个问题怎么解决
  • ¥15 comsol电力电缆载流量仿真
  • ¥15 webSocket可以接TCP socket接口吗
  • ¥60 mpi并行出错,CFD++计算
  • ¥15 c#:vsto,powerpoint的外接程序中换主题颜色
  • ¥15 状态机/汽车转向灯/Sateflow
  • ¥15 这个有点复杂 有没有人看看
  • ¥15 用python如何确定子孙元素在父元素中的位置
  • ¥15 obj文件滤除异常高程
  • ¥15 用mathematicas或者matlab计算三重积分