【发布时间】:2016-11-25 01:08:46
【问题描述】:
我编写了将患者添加到链接列表的程序。现在我能够得到输出。我调整了我的代码,以便它根据严重程度从高到低插入患者,如果严重程度相同,则根据时间。我的患者类具有三个属性名称、到达和严重程度。
编辑 3
我在Patient 类中添加了compareSeverity 方法。
public boolean compareSeverity(Patient other) {
boolean result = false;
if(other.severity > severity) {
result = true;
} else if(other.severity == severity) {
if(other.arrival > arrival) {
result = true;
} else {
result = false;
}
} else {
result = false;
}
return result;
}
这是PatientNode 代码 sn-p。
class PatientNode {
public Patient data;
public PatientNode next;
public PatientNode(Patient data, PatientNode next) {
this.data = data;
this.next = next;
}
}
这是linked list 类中的add 方法。
public void add(String name, int severity) {
lastArrival++;
Patient patient = new Patient(name, lastArrival, severity);
PatientNode current, previous;
current = head;
previous = null;
if(head == null) {
head = current = new PatientNode(patient, head);
size++;
} else {
while(current!=null) {
//previous = current;
if(current.data.compareSeverity(patient)) {
PatientNode n = new PatientNode(patient,current);
size++;
n.next = current;
if(previous==null) {
head = n;
} else {
previous.next = n;
}
return;
}
previous = current;
current = current.next;
}
}
}
我现在得到的输出是这样的,但是当问题似乎与 same severity 患者有关时。
我希望我的输出如下所示:
患者 1,到达 2,严重程度 3
患者 2,到达 3,严重程度 3
或者如果他们有不同的严重程度,那么像这样:
患者 1,到达 2,严重程度 2
患者 2,到达 1,严重程度 1
简而言之,severity 必须按降序排列,如果严重性相同则按照arrival 升序排列。
任何关于我如何根据严重程度存储患者的想法/指针或一些伪代码都会非常棒,谢谢。
【问题讨论】:
-
您需要自己实现吗?你真的应该使用
TreeSet。 -
是的,我必须自己实现。 @4castle
标签: java oop collections linked-list nodes