0

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]

Where is my code wrong?

4

4 回答 4

0

我建议你写

 PriorityQueue<Issue> issues =  new PriorityQueue<Issue>();

并使用 Comparable 实现类问题。

于 2012-08-16T17:07:17.383 回答
0

根据此代码,您将在优先级较低的问题之后插入新问题。

while (tempQ.next != null
              && tempQ.issue.getPriority() >   newIssue.getPriority()) {
    tempQ = tempQ.next;
}

newIssueQ.next = tempQ.next;
tempQ.next = newIssueQ;
于 2012-08-16T16:12:18.147 回答
0

您应该将队列与控制逻辑分开。这样您就不会因为需要由内而外思考这一事实而感到困惑。我敢肯定,你会很容易得到这个错误。

如果您无法记住您的代码-> 重构。

于 2012-08-16T16:12:55.390 回答
0

你为什么不使用Collections.sort()你自己的基于优先级的compareTo()

您实质上是在要求我们使用调试器来单步执行您的代码。但这看起来很奇怪

 newIssueQ.next = tempQ.next;
 tempQ.next = newIssueQ;

看你甚至没有检查所有的优先级。我知道这会遍历列表,但是...尝试进入调试器,您会看到。

else if (issue.getPriority() > newIssue.getPriority()) {
          while (tempQ.next != null
                  && tempQ.issue.getPriority() >   newIssue.getPriority()) {
              tempQ = tempQ.next;
          }
于 2012-08-16T15:57:53.323 回答