Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a method in Java public static void swapWithNext( Node beforep ) to implem

ID: 3848040 • Letter: W

Question

Write a method in Java

public static void swapWithNext( Node beforep )

to implement a swap between two adjacent elements by adjusting only the links (and not the data) using a single linked list.

Notes:

beforep is an input parameter that represents the node before the two adjacent nodes that are to be swapped.

For this problem consider the following class Node:

private class Node {

public Object data; public Node next;

public Node(Object data, Node next) { this.data = data;

this.next = next;

}

}

Explanation / Answer


public class Node {
   public Object data;
   public Node next;
   Node(){
       this.data=null;
       this.next=null;
   }
   Node(Object data,Node next){
       this.data=data;
       this.next=next;
   }
  
public Object getdata(){
   return data;
}
}

allmain.java
import java.util.*;
import java.awt.*;
public class allmain {
   static Node head=null;
   static int size=0;
   public static void main(String args[]){
      
       Node node1=new Node(1,null);
       Node node2=new Node(2,null);
       Node node3=new Node(3,null);
       Node node4=new Node(4,null);
       head=node1;
       head.next=node2;
       node2.next=node3;
       node3.next=node4;
       Node curent=head;
       while(curent!=null){
           System.out.print(curent.data+" ");
       curent=curent.next;
       }
       Node curents=head;
       while(curents!=null){
           size++;
       curents=curents.next;
       }
       swapwithnext(head);
  
  
   }
   public static void swapwithnext(Node befrep){
       int icr=0;
       Node temp=new Node(10,null);
       Integer befdata=(Integer)befrep.data;
       Node curentss=head;
               while(curentss!=null){
                   Integer data=(Integer)curentss.data;
           if(data!=befdata){
               icr++;
           }
       curentss=curentss.next;
       }
       if(icr==size){
           System.out.println("final node swapping not allowed:");
           return;
       }
       else{
   Node curent2=head;
   while(curent2!=null)
   {
       Integer ele=(Integer)curent2.data;
       if(ele==befdata){
           temp.data=curent2.data;
           curent2.data=curent2.next.data;
           curent2.next.data=temp.data;
           break;
          
       }
       curent2=curent2.next;
   }
       }
   Node   curentfinal=head;
   System.out.println(" after swapping:");
       while(curentfinal!=null){
           System.out.print(curentfinal.data+" ");
       curentfinal=curentfinal.next;
       }
   }

}
output:

1 2 3 4
after swapping:
2 1 3 4

allmain.java
import java.util.*;
import java.awt.*;
public class allmain {
   static Node head=null;
   static int size=0;
   public static void main(String args[]){
      
       Node node1=new Node(1,null);
       Node node2=new Node(2,null);
       Node node3=new Node(3,null);
       Node node4=new Node(4,null);
       head=node1;
       head.next=node2;
       node2.next=node3;
       node3.next=node4;
       Node curent=head;
       while(curent!=null){
           System.out.print(curent.data+" ");
       curent=curent.next;
       }
       Node curents=head;
       while(curents!=null){
           size++;
       curents=curents.next;
       }
       swapwithnext(node2);
  
  
   }
   public static void swapwithnext(Node befrep){
       int icr=0;
       Node temp=new Node(10,null);
       Integer befdata=(Integer)befrep.data;
       Node curentss=head;
               while(curentss!=null){
                   Integer data=(Integer)curentss.data;
           if(data!=befdata){
               icr++;
           }
       curentss=curentss.next;
       }
       if(icr==size){
           System.out.println("final node swapping not allowed:");
           return;
       }
       else{
   Node curent2=head;
   while(curent2!=null)
   {
       Integer ele=(Integer)curent2.data;
       if(ele==befdata){
           temp.data=curent2.data;
           curent2.data=curent2.next.data;
           curent2.next.data=temp.data;
           break;
          
       }
       curent2=curent2.next;
   }
       }
   Node   curentfinal=head;
   System.out.println(" after swapping:");
       while(curentfinal!=null){
           System.out.print(curentfinal.data+" ");
       curentfinal=curentfinal.next;
       }
   }

}
output:
1 2 3 4
after swapping:
1 3 2 4

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote