Java question To easy understand the code Please provide com

Java question. To easy understand the code. Please, provide comments and all the steps.


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 single
linked list.

// beforep is the cell before the two adjacent cells that are to be swapped.
public static void swapWithNext( Node beforep )
{
Node p, afterp;

}
Note: Assume that both p and afterp are not null.

Solution

Please follow the code and comments for description :

CODE :

// beforep is the cell before the two adjacent cells that are to be swapped.
public static void swapWithNext( Node beforep ) {

   Node p, afterp; // local variables
   p = beforep.next; // get the next value to the variable
   afterp = p.next; // Both p and afterp assumed not null.
   p.next = afterp.next; // the next value is updated with the succeeding value of the current node
   beforep.next = afterp; // update the value to the current value
   afterp.next = p; // save the value current value

}

Hope this is helpful.

Java question. To easy understand the code. Please, provide comments and all the steps. Complete this Java method so that it properly implements a swap between

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site