Please provide all the steps to solve and test these methods
Please; provide all the steps to solve and test these methods, and comments to easy understand the code.
---Java problems--
(1) Complete; Java method so that it properly implements a swap between two
adjacent elements by adjusting only the links (and not the data) using a single
linked list.
--
// beforep is the cell before the two adjacent cells that are to be swapped.
// Assume that both p and afterp are not null.
public static void swapWithNext( Node beforep )
{
Node p, afterp;
....
....
....
}
(2) Complete; 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.
// Assume that p, beforep, and afterp are not null.
public static void swapWithNext( Node p )
{
Node beforep, afterp;
.....
.....
.....
}
Solution
Please find my imlementation.
1)
public static void swapWithNext( Node beforep )
{
Node p, afterp;
p = beforep.next;
afterp = p.next;
Node temp = afterp.next; // list after \'afterp\'
beforep.next = afterp;
afterp.next = p;
p.next = temp;
}
2)
public static void swapWithNext( Node p )
{
Node beforep, afterp;
beforep = p.prev;
afterp = p.next;
Node temp = afterp.next;
beforep.next = afterp;
afterp.prev = beforep;
afterp.next = p;
p.prev = afterp;
p.next = temp;
temp.prev = p;
}

