简体   繁体   中英

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). 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:

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. Since this is Java, your temp (r in this case) has just a reference to q. That's why when you change write q = p; , r also changes entirely. 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 . Hope this helps.

Is it possible to use PriorityQueue instead of implementing sort function?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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