/* package whatever; // don't place package name! */
import java.util.* ;
import java.lang.* ;
import java.io.* ;
class LinkedPatients {
private PatientNode head = null ;
private int size = 0 ;
private int lastArrival = 0 ;
public PatientNode getHead( ) {
return head;
}
public int getSize( ) {
return size;
}
public int getLastArrival( ) {
return lastArrival;
}
public void add
( String name,
int severity
) { Patient patient;
lastArrival++;
patient = new Patient( name, lastArrival, severity) ;
PatientNode n = new PatientNode( patient, null ) ;
if ( head== null ) {
head = n;
size++;
return ;
} else if ( n.data .getSeverity ( ) > head.data .getSeverity ( ) ) {
n.next = head;
size++;
head = n;
return ;
}
PatientNode p = head;
boolean added = false ;
while ( p.next != null && ! added) {
if ( p.next .data .getSeverity ( ) < severity) {
n.next = p.next ;
p.next = n;
added = true ;
} else if ( p.next .data .getSeverity ( ) == severity && p.next .data .getArrival ( ) > lastArrival) {
n.next = p.next ;
p.next = n;
added = true ;
}
p= p.next ;
}
if ( ! added) {
p.next = n;
}
size++;
}
}
class PatientNode {
public Patient data;
public PatientNode next;
public PatientNode( Patient data, PatientNode next) {
this .data = data;
this .next = next;
}
}
class Patient{
private int severity;
private int arrival;
public Patient
( String name,
int arrival,
int severity
) { this .name = name;
this .severity = severity;
this .arrival = arrival;
}
public int getSeverity( ) {
return severity;
}
public int getArrival( ) {
return arrival;
}
@Override
return "Patient " + this .name + " arrived at " + this .arrival + " with severity " + this .severity ;
}
}
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
caseOne( ) ;
caseTwo( ) ;
caseThree( ) ;
}
private static void caseOne( ) {
LinkedPatients hospi = new LinkedPatients( ) ;
hospi.add ( "Ihor" , 1 ) ;
hospi.add ( "Casper" , 3 ) ;
hospi.add ( "Clayton" , 2 ) ;
hospi.add ( "Zelma" , 1 ) ;
printHospi( hospi) ;
}
private static void caseTwo( ) {
LinkedPatients hospi = new LinkedPatients( ) ;
hospi.add ( "Zelma" , 1 ) ;
hospi.add ( "Clayton" , 2 ) ;
hospi.add ( "Casper" , 3 ) ;
hospi.add ( "Ihor" , 1 ) ;
hospi.add ( "Edwina" , 3 ) ;
printHospi( hospi) ;
}
private static void caseThree( ) {
LinkedPatients hospi = new LinkedPatients( ) ;
hospi.add ( "Zelma" , 1 ) ;
hospi.add ( "Clayton" , 2 ) ;
hospi.add ( "Ihor" , 1 ) ;
hospi.add ( "Edwina" , 3 ) ;
printHospi( hospi) ;
}
public static void printHospi( LinkedPatients hospi) {
System .
out .
println ( "Size = " + hospi.
getSize ( ) ) ; System .
out .
println ( "LastArrival = " + hospi.
getLastArrival ( ) ) ; PatientNode current = hospi.getHead ( ) ;
while ( current != null ) {
System .
out .
println ( current.
data ) ; current = current.next ;
}
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgpjbGFzcyBMaW5rZWRQYXRpZW50cyB7Cglwcml2YXRlIFBhdGllbnROb2RlIGhlYWQgPSBudWxsOwoJcHJpdmF0ZSBpbnQgc2l6ZSA9IDA7Cglwcml2YXRlIGludCBsYXN0QXJyaXZhbCA9IDA7CgkKCXB1YmxpYyBQYXRpZW50Tm9kZSBnZXRIZWFkKCl7CgkJcmV0dXJuIGhlYWQ7Cgl9CgkKCXB1YmxpYyBpbnQgZ2V0U2l6ZSgpewoJCXJldHVybiBzaXplOwoJfQoJCglwdWJsaWMgaW50IGdldExhc3RBcnJpdmFsKCl7CgkJcmV0dXJuIGxhc3RBcnJpdmFsOwoJfQoJCglwdWJsaWMgdm9pZCBhZGQoU3RyaW5nIG5hbWUsIGludCBzZXZlcml0eSkgewoJCVBhdGllbnQgcGF0aWVudDsKCQkKCQlsYXN0QXJyaXZhbCsrOwoJCXBhdGllbnQgPSBuZXcgUGF0aWVudChuYW1lLCBsYXN0QXJyaXZhbCwgc2V2ZXJpdHkpOwoJCVBhdGllbnROb2RlIG4gPSBuZ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