fork download
  1.  
  2. class Course {
  3. private static final int NUL = -1;
  4. private AGradeListNode[] nodes;
  5. private int list;
  6. private int free;
  7.  
  8. protected static class AGradeListNode {
  9. protected float grade;
  10. protected Object student;
  11. protected int next;
  12.  
  13. @Override
  14. public String toString() {
  15. return "Student: " + this.student + " Grade: " + this.grade;
  16. }
  17. }
  18. public Course(int maxNumStudents) {
  19. this.nodes = new AGradeListNode[maxNumStudents];
  20. for (int idx = 0; idx < maxNumStudents; idx++)
  21. this.nodes[idx] = new AGradeListNode();
  22. for (int idx = 1; idx < maxNumStudents; idx++)
  23. this.nodes[idx - 1].next = idx;
  24. this.nodes[maxNumStudents - 1].next = NUL;
  25.  
  26. this.list = NUL;
  27. this.free = 0;
  28. }
  29. private int getNode() {
  30. if (this.free == NUL)
  31. throw new IllegalStateException("Course is full");
  32. int idx = this.free;
  33. this.free = this.nodes[idx].next;
  34. return idx;
  35. }
  36. public void addStudentGrade(Object stu, float grade) {
  37. // Search for insertion point (i.e. between prevIdx and nextIdx)
  38. int prevIdx = NUL;
  39. int nextIdx = this.list;
  40. while (nextIdx != NUL && this.nodes[nextIdx].grade >= grade) {
  41. prevIdx = nextIdx;
  42. nextIdx = this.nodes[nextIdx].next;
  43. }
  44.  
  45. // Initialize and insert new node
  46. int newIdx = getNode();
  47. this.nodes[newIdx].student = stu;
  48. this.nodes[newIdx].grade = grade;
  49. this.nodes[newIdx].next = nextIdx;
  50. if (prevIdx == NUL)
  51. this.list = newIdx;
  52. else
  53. this.nodes[prevIdx].next = newIdx;
  54. }
  55. public void print() {
  56. for (int idx = this.list; idx != NUL; idx = this.nodes[idx].next)
  57. System.out.printf("%2d: %s%n", idx, this.nodes[idx]);
  58. System.out.println();
  59. }
  60. public static void main(String[] args) {
  61. Course course = new Course(10);
  62. course.addStudentGrade("Student 1", 3f); course.print();
  63. course.addStudentGrade("Student 2", 5f); course.print();
  64. course.addStudentGrade("Student 3", 2f); course.print();
  65. course.addStudentGrade("Student 4", 1f); course.print();
  66. course.addStudentGrade("Student 5", 2f); course.print();
  67. course.addStudentGrade("Student 6", 3f); course.print();
  68. }
  69. }
Success #stdin #stdout 0.05s 4386816KB
stdin
Standard input is empty
stdout
 0: Student: Student 1   Grade: 3.0

 1: Student: Student 2   Grade: 5.0
 0: Student: Student 1   Grade: 3.0

 1: Student: Student 2   Grade: 5.0
 0: Student: Student 1   Grade: 3.0
 2: Student: Student 3   Grade: 2.0

 1: Student: Student 2   Grade: 5.0
 0: Student: Student 1   Grade: 3.0
 2: Student: Student 3   Grade: 2.0
 3: Student: Student 4   Grade: 1.0

 1: Student: Student 2   Grade: 5.0
 0: Student: Student 1   Grade: 3.0
 2: Student: Student 3   Grade: 2.0
 4: Student: Student 5   Grade: 2.0
 3: Student: Student 4   Grade: 1.0

 1: Student: Student 2   Grade: 5.0
 0: Student: Student 1   Grade: 3.0
 5: Student: Student 6   Grade: 3.0
 2: Student: Student 3   Grade: 2.0
 4: Student: Student 5   Grade: 2.0
 3: Student: Student 4   Grade: 1.0