これが基本クラスです。Issue(priority, description)
次のようなリンク リストの実装があります。
public class IssueQueue {
public Issue issue;
public IssueQueue next;
private static int count = 0;
public IssueQueue() {
}
public IssueQueue(Issue i) {
issue = i;
next = null;
}
public int getCount() {
return count;
}
public void Add(Issue newIssue) {
IssueQueue tempQ = this;
IssueQueue newIssueQ = new IssueQueue(newIssue);
if (count == 0) {
issue = newIssue;
} else if (issue.getPriority() > newIssue.getPriority()) {
while (tempQ.next != null
&& tempQ.issue.getPriority() > newIssue.getPriority()) {
tempQ = tempQ.next;
}
newIssueQ.next = tempQ.next;
tempQ.next = newIssueQ;
} else if (issue.getPriority() <= newIssue.getPriority()) {
newIssueQ.issue = issue;
newIssueQ.next = next;
issue = newIssue;
next = newIssueQ;
}
count++;
}
public void print() {
IssueQueue tempQ = this;
if (next == null) {
System.out.println(issue);
} else {
do {
System.out.println(tempQ.issue);
tempQ = tempQ.next;
} while (tempQ.next != null);
System.out.println(tempQ.issue);
}
System.out.println("--------------------");
}
}
問題は、次のことです: iq.Add(new Issue(10, "Issue 1")); iq.print();
iq.Add(new Issue(4, "Issue 2"));
iq.print();
iq.Add(new Issue(20, "Issue 3"));
iq.print();
iq.Add(new Issue(2, "Issue 4"));
iq.print();
iq.Add(new Issue(12, "Issue 5"));
iq.print();
出力は 4 回目の挿入まで正しいです。
Issue[Priority: 20, Description: Issue 3]
Issue[Priority: 10, Description: Issue 1]
Issue[Priority: 4, Description: Issue 2]
Issue[Priority: 2, Description: Issue 4]
しかし、5 回目の挿入では次のようになります。
Issue[Priority: 20, Description: Issue 3]
Issue[Priority: 10, Description: Issue 1]
Issue[Priority: 12, Description: Issue 5]
Issue[Priority: 4, Description: Issue 2]
Issue[Priority: 2, Description: Issue 4]