简体   繁体   English

如何在两个节点之间交换数据? (爪哇)

[英]How do you swap data between two nodes? (Java)

I've been working on a project using Nodes and for this project we have to order the Nodes based on the priority (0 - 2 with 0 being the highest and 2 being the lowest) that the item within the Nodes hold (in decreasing order).我一直在研究一个使用节点的项目,对于这个项目,我们必须根据节点中的项目持有的优先级(0 - 2,0 是最高的,2 是最低的)对节点进行排序(按降序) )。 Now I have got this working partially as when it actually does the swap it turns both items to be the same (I'm only trying to swap the items).现在我已经部分工作了,因为当它实际进行交换时,它会将两个项目变成相同的(我只是想交换项目)。 For example: I would enter Node_1 which holds prio of 0, then Node_2 which holds prio of 1, it should then be ordered as:例如:我将输入保存 prio 为 0 的 Node_1,然后输入保存 prio 为 1 的 Node_2,然后应将其排序为:

Node_2(holds prio 1) --> Node_1(holds prio 0)

then it should run the method to order them as (in decreasing priority):然后它应该运行该方法将它们排序为(按优先级递减):

Node_1(holds prio 0) --> Node_2(holds prio 1)

but instead it just turns both nodes to be the same (same priority):但它只是将两个节点变成相同的(相同的优先级):

Node_1(holds prio 0) --> Node_1(holds prio 0)

this stays the same no matter how many nodes I add to it.无论我添加多少节点,它都保持不变。 They all turn to the same Node with the highest priority.它们都转向具有最高优先级的同一个节点。 Any help would be great.任何帮助都会很棒。

Code:代码:

private void sortJobs() {
    Node p, q, r;

    p = jobs;
    q = null;
    r = null;
    //only runs if there is more than 1 job
    while (p != null && p.next != null) {
        q = p;
        p = p.next;

        if (q.item.getPriority() > p.item.getPriority()) {
            r = q;
            q.item = p.item;
            p.item = r.item;


        }

    }

}

Please let me know if there is a better way to do this as I'm still learning.请让我知道是否有更好的方法来做到这一点,因为我仍在学习。

Something more like this maybe:也许更像这样的事情:

private void sortJobs(Node p) {

    if (p != null && p.next != null) {

        Node q = p.next;

        Item pItem = p.getItem();
        Item qItem = q.getItem();

        // check for nulls???  Safety???
        if (qItem.getPriority() < pItem.getPriority()) {

            p.setItem(qItem);
            q.setItem(pItem);

         }

         // almost forgot the recursion
         sortJobs(q);

    }

}

You keep the nodes in the same order, but you swap the items in them.您将节点保持在相同的顺序,但交换其中的项目。 We don't need the copy into a third dummy value trick here because we already have references to the two items (we needed the references anyway to grab the priorities).我们不需要复制到第三个虚拟值技巧,因为我们已经有了对这两个项目的引用(无论如何我们都需要引用来获取优先级)。

This has been said in Andreas's answer, but i'll elaborate further.这在 Andreas 的回答中已经说过,但我会进一步详细说明。 Since this is Java, your temp (r in this case) has just a reference to q.由于这是 Java,因此您的 temp(在本例中为 r)只有对 q 的引用。 That's why when you change write q = p;这就是为什么当你改变时写q = p; , r also changes entirely. , r 也完全改变。 Because they reference the same instance of the object.因为它们引用了相同的对象实例。 You need to create a temporary item to swap properly.您需要创建一个临时项目才能正确交换。 The correct fix would be to have r be an item object, then setting p.item = r .正确的解决方法是让 r 成为一个 item 对象,然后设置p.item = r Hope this helps.希望这可以帮助。

Is it possible to use PriorityQueue instead of implementing sort function?是否可以使用 PriorityQueue 而不是实现排序功能?

PriorityQueue<Node> pq = new PriorityQueue(new Comparator<Node>(){
    public int compare(Node a, Node b){
        return a.item.getPriority()-b.item.getPriority();
    }
});
p=jobs;
if(p==null) return p;
//add all jobs in PriorityQueue
while (p != null) {
    pq.add(p);
    p=p.next;
}
//Change next pointer
Node head=pq.poll();
Node prev=head;
while(pq.size()>0){
    Node n = pq.poll;
    prev.next=n;
    prev=n;
}
return head;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM