數據結構-單鏈表與排序(選做)


任務要求

  • 補充MyList.java的內容,提交運行結果截圖(全屏),課下推送代碼到碼雲
public class MyList {
	public static void main(String [] args) {
		//選用合適的構造方法,用你學號前后各兩名同學的學號創建四個結點
		
		
		//把上面四個節點連成一個沒有頭結點的單鏈表
		
		//遍歷單鏈表,打印每個結點的

		//把你自己插入到合適的位置(學號升序)

		//遍歷單鏈表,打印每個結點的

		//從鏈表中刪除自己

		//遍歷單鏈表,打印每個結點的
	}
}
public class Node<T>                             //單鏈表結點類,T指定結點的元素類型
{
    public T data;                               //數據域,存儲數據元素
    public Node<T> next;                         //地址域,引用后繼結點

    public Node(T data, Node<T> next)            //構造結點,data指定數據元素,next指定后繼結點
    {
        this.data = data;                        //T對象引用賦值
        this.next = next;                        //Node<T>對象引用賦值
    }
    public Node()
    {
        this(null, null);
    }
    public String toString()                     //返回結點數據域的描述字符串
    {
        return this.data.toString();
    }
}
  • 在數據結構和算法中,排序是很重要的操作,要讓一個類可以進行排序,有兩種方法:
    • 有類的源代碼,針對某一成員變量排序,讓類實現Comparable接口,調用Collection.sort(List)
    • 沒有類的源代碼,或者多種排序,新建一個類,實現Comparator接口 調用Collection.sort(List, Compatator)
      針對下面的Student類,使用Comparator編程完成以下功能:
  1. 在測試類StudentTest中新建學生列表,包括自己和學號前后各兩名學生,共5名學生,給出運行結果(排序前,排序后)
  2. 對這5名同學分別用學號和總成績進行增序排序,提交兩個Comparator的代碼
  3. 課下提交代碼到碼雲
class Student {
    private String id;//表示學號
    private String name;//表示姓名
    private int age;//表示年齡
    private double computer_score;//表示計算機課程的成績
    private double english_score;//表示英語課的成績
    private double maths_score;//表示數學課的成績
    private double total_score;// 表示總成績
    private double ave_score; //表示平均成績

    public Student(String id, String name){
        this.id = id;
        this.name = name;
}
    public Student(String id, String name, char sex, int age){
        this(id, name);
        this.sex = sex;
        this.age = age;
}
    public String getId(){
        return id;
}//獲得當前對象的學號,
    public double getComputer_score(){
        return computer_score;
}//獲得當前對象的計算機課程成績,
    public double getMaths_score(){
        return maths_score;
}//獲得當前對象的數學課程成績,
    public double getEnglish_score(){
        return english_score;
}//獲得當前對象的英語課程成績,

    public void setId(String id){
        this.id=id;
}// 設置當前對象的id值,
    public void setComputer_score(double computer_score){
        this.computer_score=computer_score;
}//設置當前對象的Computer_score值,
    public void setEnglish_score(double english_score){
        this.english_score=english_score;
}//設置當前對象的English_score值,
    public void setMaths_score(double maths_score){
        this.maths_score=maths_score;
}//設置當前對象的Maths_score值,

    public double getTotalScore(){
        return computer_score+maths_score+english_score;
}// 計算Computer_score, Maths_score 和English_score 三門課的總成績。
    public double getAveScore(){
        return getTotalScore()/3;
}// 計算Computer_score, Maths_score 和English_score 三門課的平均成績。

}

class Undergraduate extends Student{
    private String classID;

    public Undergraduate(String id, String name, char sex, int age,String classID){
        super(id,name,sex,age);
        this.classID=classID;
    }
    public String getClassID(){
        return classID;
    }
    public void setClassID(String classID){
        this.classID=classID;
    }
}

實驗結果


碼雲鏈接

https://gitee.com/J20175319/java20175319_jiangye/tree/master/week10/數據結構選做/src


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM