This is the basic class: Issue(priority, description)
I have a linked list implementation as follows:
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("--------------------");
}
}
The problem is, on doing the following: 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();
The output is correct till 4th insertion:
Issue[Priority: 20, Description: Issue 3]
Issue[Priority: 10, Description: Issue 1]
Issue[Priority: 4, Description: Issue 2]
Issue[Priority: 2, Description: Issue 4]
But on 5th Insertion it becomes:
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]