按学生ID对LinkedList进行排序

时间:2014-12-10 06:28:18

标签: java sorting linked-list

我仍然是一个非常新手的程序员,所以请尽量描述。我在让我的所有代码都为我的作业工作时遇到了问题。该作业为我提供了四个不需要更改的完整文件(StudentIf.java,StudentCollectionIf.java,StudentLLNode.java和StudentRecords.java)以及一个带有ID和五个等级的名称列表的测试文本文件。

指派分配说明:https://www.cs.colostate.edu/~cs161/Fall14/more_assignments/P5/P5.html

我修改过的两个文件是Student.java和StudentLL.java。同样在我的运行配置 - 参数中,我有" cs161 5"。我知道很多,但任何帮助都会很棒。

我知道insort方法根本不是真正干净的代码,但我无法解决任何问题。

我目前正在获得正确的输出,但它们没有排序。

Top Score: 0.0
Avg Score: 0.0
Course: cs161
Adam    2143  85 95 85 75 65    score: 81.00
John    1243  60 70 80 55 55    score: 64.00
Mick    1324  70 60 70 80 90    score: 74.00
Ellen   2341  90 95 88 77 66    score: 83.20
Jim     1234  50 40 50 60 70    score: 54.00
Lena    1423  99 50 90 90 85    score: 82.80
Leila   1432  60 70 60 70 60    score: 64.00
Mike    1342  60 70 80 90 99    score: 79.80
Ada     2134  90 90 90 90 90    score: 90.00
Helen   2314  89 79 99 89 88    score: 88.80

代码:

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class StudentLL implements StudentCollectionIF{
    private String course;
    private StudentLLNode head;
    private int size;
    private boolean debug;  // you can set debug in main

    // the client code provides the course name
    public StudentLL(String course){
        this.course = course;
    }

    public String toString(){
        String res = "Course: " + course + "\n";
        for(StudentLLNode curr = head; curr !=null; curr=curr.getNext()){
             StudentIF nS = curr.getStd();
             res = res + nS + "\n";
        }
        return res;
    }

    @Override
    public boolean insort(StudentIF s) {
        StudentLLNode curr = head;

        if (s == null) {
            return false;
        }
        if (head == null) {
            StudentLLNode student = new StudentLLNode(s);
            head = student;
            size++;
            //System.out.println("working");
            return true;
        } else {
            if (curr.getStd().compareTo(s) == 0) {
                return false;
            }

            Map<Integer, StudentLLNode> tcur = new TreeMap<Integer, StudentLLNode>();
            tcur.put(curr.getStd().getId(), curr);
            while (curr.getNext() != null) {
                curr = curr.getNext();
                if(curr.getStd().compareTo(s) == 0) {
                    return false;
                }
                tcur.put(curr.getStd().getId(), curr);
            }
            curr.setNext(new StudentLLNode(s));
            tcur.put(curr.getStd().getId(), curr.getNext());
            System.out.println(s.getName() + " " + s.getId());
            ArrayList<StudentLLNode> bs = new ArrayList<StudentLLNode>();
            Iterator it = tcur.entrySet().iterator();
            while(it.hasNext()) {
                Map.Entry pairs = (Map.Entry)it.next();
                StudentLLNode sss = (StudentLLNode) pairs.getValue();
                bs.add(sss);
                it.remove();
            }

            for(int i = 0; i < bs.size(); i++) {
                //bs.get(i).setNext(bs.get(i + 1));
                if(i == bs.size() - 1) {
                    //bs.get(i).setNext(null);
                } else {
                    //bs.get(i).setNext(bs.get(i + 1));
                }
                System.out.println("\t" + bs.get(i).toString() + " NET " + bs.get(i).getNext() );
            }

            //bs.get(bs.size() - 1).setNext(null);
            //confused here
            /*StudentLLNode student1 = new StudentLLNode(s);
            curr.setNext(student1);*/
            size++;
            return true;
        }
    }

    @Override
    public boolean remove(StudentIF s) {
        StudentLLNode current = head;
        if(s == null){
            return false;
        }
        if(s.equals(current.getStd())){
            //StudentLLNode  top = head;
            head = head.getNext();
            size--;
            return true;
        }
        else{
            StudentLLNode previous, next;
            previous = current;
            current = current.getNext();
            while(current != null){
               next = current.getNext();
               if(s.getId() == (current.getStd().getId())){  
                    previous.setNext(next);
                    size--;
                    return true;
                }
               previous = current;
               current = next;
            }
        }
        return false;
      }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return size;
    }

    @Override
    public double classAvg() {
//      int count = 0, total = 0;
//      for (int i = 1; i < 5; i++)
//      {
//          total += computeScore((StudentLLNode) head);
//          count++;
//      }
//      gettop();
//      return total / count;
        return 0;
    }

    @Override
    public double classTopScore() {
        return 0;
    }
    }

======

import java.text.DecimalFormat;
import java.util.Arrays;

public class Student implements StudentIF{

    private String name;
    private int id;
    private int[] grades;
    private int numGrades;
    private int totalGrades;

    // The constructor
    //    initializes the instance variables  
    //    name, id, grades = new int[totalGrades], and numGrades = 0;
    public Student (String name, int id, int totalGrades){
        this.name = name;
        this.id = id;
        grades = new int[totalGrades];
        this.totalGrades = totalGrades;
        numGrades= 0;

       //System.out.println(name+"  "+id+"   "+grades[0]+" "+grades[1]+" "+grades[2]+" "+grades[3]+" "+grades[4]+" "+"   "+totalGrades);
    }

    public String toString() {
        String res = name + "\t" + id + " ";
        for (int i=0; i < totalGrades; i++) {
            res += " " + grades[i];
        }
        res += "\tscore: " + new DecimalFormat("0.00").format(computeScore());
        return res;
    }

    @Override
    public int compareTo(StudentIF arg0) {
        if (arg0 == null || arg0.getId() != id) {
            return 1;
        }

        /*if (this.id > arg0.getId())
            return 1;
        else if (this.id < arg0.getId())
            return -1;*/
        /*else {
            return 0;
        }*/

        return 0;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public int getId() {
        return id;
    }

    @Override
    public double computeScore() {
        double total = 0;
          if (numGrades == 0) {
            return total;
          }
          if (numGrades > grades.length) {
            numGrades = grades.length;
          }
          for (int i = 0; i < numGrades; i++) {
            total += grades[i];
          }
          return total / grades.length;
    }

    @Override
    public boolean addGrade(int newGrade) {
        if(numGrades<grades.length){
            grades[numGrades] = newGrade;
            numGrades++;
            return true;
        }
        return false;
    }


    @Override
    public boolean equals(StudentIF other) {
        if (other == null) {
            return false;
        }
        if (this.id == other.getId()) {
            return true;
        }
        return false;
    }

}

2 个答案:

答案 0 :(得分:0)

让您的Student类实现Comparable接口并覆盖compareTo方法

Class Student implements Comparable<Student>
{
private int id;

@Override
public int compareTo(Student s) {

    if (this.id > s.id)
        return 1;
    else if (this.id < s.id)
        return -1;
    else
        return 0;
}

}

然后,使用Collections.sort(yourlisthere),您将获得根据学生id

排序的列表

答案 1 :(得分:0)

为了扩展@Abishek的回答(因为我没有足够的回复评论),compareTo方法可以简化为

public int compareTo(Student s) {
    return this.id - s.id;
 }

您不需要特别返回1,-1或0,因为您可能会从该答案中推断出来。如果this.id比s.id“更大”,则为正数,反之则为负数,如果它们相等则为0。