import java.util.*;
class Node {
public int data;
public Node next;
public Node(int value, Node address) {
data = value;
next = address;
}
public Node(int value) {
data = value;
next = null;
}
public Node() {
data = 0;
next = null;
}
}
public class Main {
public static Node array_to_LL(int[] arr) {
if (arr.length == 0){
return null;
}
Node head=new Node(arr[0]);
Node temp=head;
for(int i=1;i<arr.length;i++){
Node curr=new Node(arr[i]);
temp.next=curr;
temp=temp.next;
}
return head;
}
public static void transverse(Node current){
Node temp=current;
while(temp != null){
System.out.println(temp.data);
temp=temp.next;
}
}
public static Node insert_start(Node head){
Node temp=head;
Node val=new Node(98);
val.next=temp;
return val;
}
public static Node insert_index(Node head){
int index=1;
int count=1;
Node temp=head;
while(temp!=null && temp.next.next!=null){
if(index==count){
Node curr=new Node(90);
curr.next=temp.next;
temp.next=curr;
break;
}
count++;
temp=temp.next;
}
return head;
}
public static Node inset_end(Node head){
Node temp=head;
Node val=new Node(98);
while (temp.next!=null) {
temp=temp.next;
}
temp.next=val;
return head;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
Node head = array_to_LL(arr);
transverse(head);
int index=sc.nextInt();
System.out.println("Enter the index");
if(index==0){
head=insert_start(head);
}else if(index==1){
head=insert_index(head);
}else{
head=inset_end(head);
}
transverse(head);
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: