Can someone explain to me each line of code in comments? import java.util.*; pub
ID: 3722298 • Letter: C
Question
Can someone explain to me each line of code in comments?
import java.util.*;
public class insertBeforeM {
Node head; // head of list
/* Linked list Node*/
class Node
{
int data;
Node next;
Node(int d) {data = d; next = null; }
}
/* Inserts a new Node at front of the list. */
public void append(int new_data)
{
/* 1. Allocate the Node &
2. Put in the data
3. Set next as null */
Node new_node = new Node(new_data);
/* 4. If the Linked List is empty, then make the
new node as head
*/
if (head == null)
{
head = new Node(new_data);
return;
}
/* 4. This new node is going to be the last node, so
make next of it as null */
new_node.next = null;
/* 5. Else traverse till the last node */
Node last = head;
while (last.next != null)
last = last.next;
/* 6. Change the next of last node */
last.next = new_node;
return;
}
public void insertBefore(int target, int new_data)
{
Node new_node,save,pred = null;
int value;
new_node = new Node(new_data);//creating a newnode and putting the value.
if(head==null)//checking for empty.
{
System.out.println("Specified not not found.");
}else//otherwise checking for target data and also checking to null
{
save=head;
while(target!=save.data&&save.next!=null)
{
pred=save;
save=save.next;
}
if(save.data==target)//if target found then insert.
{
if(head==save){
new_node.next=save;
head=new_node;
}
else
{
new_node.next=save;
pred.next=new_node;
}
}
else
{
System.out.println("Specified node not found");
}
}
}
public void printList()
{
Node tnode = head;
while (tnode != null)
{
System.out.print(tnode.data+" ");
tnode = tnode.next;
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
insertBeforeM list=new insertBeforeM();
list.append(10);
list.append(6);
list.append(8);
list.append(9);
list.append(16);
System.out.print("Your list is : ");
list.printList();
System.out.print(" Enter a node before you want to insert a value :");
int target=sc.nextInt();
System.out.print(" Now enter a value :");
int value=sc.nextInt();
list.insertBefore(target, value);
System.out.print(" Your List after insertion :");
list.printList();
}
}
Explanation / Answer
ANSWER: Here in the code it has the sufficient comments to understand it but I think you are the beginner so I am explaining the concept of this code here in the class insert before as the class says here we want to insert a node in a linked list data structure.Here in the class Node which is an inner class used to create a node for our requirement and we create an object of this class we need a node in the code. In the append method we have to insert a node after a node means simply appending so first we check that list is empty or not as if (head == null) by this before we make node and put the data for insert at the end, in the if statemnet we know that if this statemnt is true then control will come into this then here we used this line head = new Node(new_data); as it says that we poniting head to the newnode and after that in the rest code we traverse the linked at the end using while (last.next != null) this and when we found the end then we insert the new node at the end.
Now I am explaining the function insertBefore() function fist of all we are creating a new node and putting the value. and checking for the list is empty or not by the if statement.after that we save the head value for future purpose and by while(target!=save.data&&save.next!=null) we reach the node in which node we have to insert a node here we use pred object node to store the previous value at the target node and by if(save.data==target) this statemnt we are confirmin that we got that node with comparing values means we found our target, if(head==save) says that we found target node at the head
new_node.next=save;
head=new_node;
then by these two statements, we put the new _node.next to the head and head=new_node. an otherwise specified node at some place do the same thing at that time otherwise we did not find the target node we are printing.
In the main function, we simply appending some value and after that call our insert before the function to check.