fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. class LinkedPatients {
  8. private PatientNode head = null;
  9. private int size = 0;
  10. private int lastArrival = 0;
  11.  
  12. public PatientNode getHead(){
  13. return head;
  14. }
  15.  
  16. public int getSize(){
  17. return size;
  18. }
  19.  
  20. public int getLastArrival(){
  21. return lastArrival;
  22. }
  23.  
  24. public void add(String name, int severity) {
  25. Patient patient;
  26.  
  27. lastArrival++;
  28. patient = new Patient(name, lastArrival, severity);
  29. PatientNode n = new PatientNode(patient, null);
  30.  
  31. if(head==null) {
  32. head = n;
  33. size++;
  34. return;
  35. } else if(n.data.getSeverity() > head.data.getSeverity()) {
  36. n.next = head;
  37. size++;
  38. head = n;
  39. return;
  40. }
  41.  
  42. PatientNode p = head;
  43. boolean added = false;
  44. while(p.next!=null && !added) {
  45. if(p.next.data.getSeverity() < severity) {
  46. n.next = p.next;
  47. p.next = n;
  48. added = true;
  49. } else if(p.next.data.getSeverity()== severity && p.next.data.getArrival() > lastArrival) {
  50. n.next = p.next;
  51. p.next = n;
  52. added = true;
  53. }
  54. p=p.next;
  55. }
  56. if(!added) {
  57. p.next = n;
  58. }
  59. size++;
  60. }
  61. }
  62.  
  63. class PatientNode {
  64. public Patient data;
  65. public PatientNode next;
  66.  
  67. public PatientNode(Patient data, PatientNode next) {
  68. this.data = data;
  69. this.next = next;
  70. }
  71. }
  72.  
  73. class Patient{
  74. private String name;
  75. private int severity;
  76. private int arrival;
  77.  
  78. public Patient(String name, int arrival, int severity){
  79. this.name = name;
  80. this.severity = severity;
  81. this.arrival = arrival;
  82. }
  83.  
  84. public int getSeverity() {
  85. return severity;
  86. }
  87.  
  88. public int getArrival() {
  89. return arrival;
  90. }
  91.  
  92. @Override
  93. public String toString(){
  94. return "Patient " + this.name + " arrived at " + this.arrival + " with severity " + this.severity;
  95. }
  96. }
  97.  
  98. /* Name of the class has to be "Main" only if the class is public. */
  99. class Ideone
  100. {
  101. public static void main (String[] args) throws java.lang.Exception
  102. {
  103. caseOne();
  104. System.out.println();
  105. caseTwo();
  106. System.out.println();
  107. caseThree();
  108. System.out.println();
  109. }
  110.  
  111. private static void caseOne(){
  112. LinkedPatients hospi = new LinkedPatients();
  113. hospi.add("Ihor", 1);
  114. hospi.add("Casper", 3);
  115. hospi.add("Clayton", 2);
  116. hospi.add("Zelma", 1);
  117. printHospi(hospi);
  118. }
  119.  
  120. private static void caseTwo(){
  121. LinkedPatients hospi = new LinkedPatients();
  122. hospi.add("Zelma", 1);
  123. hospi.add("Clayton", 2);
  124. hospi.add("Casper", 3);
  125. hospi.add("Ihor", 1);
  126. hospi.add("Edwina", 3);
  127. printHospi(hospi);
  128. }
  129.  
  130. private static void caseThree(){
  131. LinkedPatients hospi = new LinkedPatients();
  132. hospi.add("Zelma", 1);
  133. hospi.add("Clayton", 2);
  134. hospi.add("Ihor", 1);
  135. hospi.add("Edwina", 3);
  136. printHospi(hospi);
  137. }
  138.  
  139. public static void printHospi(LinkedPatients hospi){
  140. System.out.println("Size = " + hospi.getSize());
  141. System.out.println("LastArrival = " + hospi.getLastArrival());
  142. PatientNode current = hospi.getHead();
  143. while(current != null){
  144. System.out.println(current.data);
  145. current = current.next;
  146. }
  147. }
  148. }
Success #stdin #stdout 0.09s 320576KB
stdin
Standard input is empty
stdout
Size = 4
LastArrival = 4
Patient Casper arrived at 2 with severity 3
Patient Clayton arrived at 3 with severity 2
Patient Ihor arrived at 1 with severity 1
Patient Zelma arrived at 4 with severity 1

Size = 5
LastArrival = 5
Patient Casper arrived at 3 with severity 3
Patient Edwina arrived at 5 with severity 3
Patient Clayton arrived at 2 with severity 2
Patient Zelma arrived at 1 with severity 1
Patient Ihor arrived at 4 with severity 1

Size = 4
LastArrival = 4
Patient Edwina arrived at 4 with severity 3
Patient Clayton arrived at 2 with severity 2
Patient Zelma arrived at 1 with severity 1
Patient Ihor arrived at 3 with severity 1