Java complete Please provide comments to easy understand it
Java complete. Please, provide comments to easy understand it.
Complete this Java method so that it properly implements a swap between two
adjacent elements by adjusting only the links (and not the data) using a doubly
linked list.
// p and afterp are cells to be switched.
public static void swapWithNext( Node p )
{
Node beforep, afterp;
}
Note: Assume that p, beforep, and afterp are not null.
Solution
Please follow the code and comments for description :
CODE :
// p and afterp are cells to be switched.
public static void swapWithNext( Node p ) {
Node beforep, afterp; // the after and before nodes for the current node
beforep = p.prev; // setting the node values to the variables
afterp = p.next;
p.next = afterp.next; // getting the succeeding value to the next value
beforep.next = afterp; // getting the next value to the current value
afterp.next = p; // interchange the values
p.next.prev = p;
p.prev = afterp;
afterp.prev = beforep; // update the values
}
Hope this is helpful.
