by Dinesh Thakur

 

import java.io.*;

class node
{
      public int v;
      public node nxt;
      public node (int x)
         {
              v=x;
         }
             public void dispval()
                  {
                      System.out.println(v);
                   }
}
 class LinkList
 {
       private node first,p,q;
       public LinkList()
         {
            first=null;
         }
             public void insertval(int x)
              {
                   node p=new node(x);
                   p.nxt=null;
                   if(first==null)
                      {
                         first=p;
                         q=p;
                      }
                            else
                              {
                                q.nxt=p;
                                q=p;
                              }
              }
          public void insmid(int x,int y)
            {
                node r=new node(x);
                p=first;
                q=first.nxt;
                while(q !=null)
                     {
                         if(q.v==y)
                           {
                              p.nxt=r;
                              r.nxt=q;
                              break;
                            }
                               else
                                {
                                   p=q;
                                   q=q.nxt;
                                }
                     }
            }
             public void displayList()
              {
                    node r=first;
                    while(r !=null)
                       {
                           r.dispval();
                           r=r.nxt;
                       }
              }
}
class InsertingElementBetween
 {
       public static void main(String args[]) throws IOException
       {
           LinkList k=new LinkList();
           BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
           String h;
           int m,n,i;
           System.out.println("How many numbers you want to insert in linked list");
           h=b.readLine();
           n=Integer.parseInt(h);
           System.out.println("Enter numericals in linked list");
           for(i=1;i<=n;i++)
                 {
                     h=b.readLine();
                     m=Integer.parseInt(h);
                     k.insertval(m);
                 }
                     System.out.println("Data in linked list is");
                     k.displayList();
                     System.out.println("Enter the element to insert in between");
                     h=b.readLine();
                     m=Integer.parseInt(h);
                     System.out.println("Enter the element before which to insert");
                     h=b.readLine();
                     n=Integer.parseInt(h);
                     k.insmid(m,n);
                     System.out.println("Data in linked list after inserting desired element is");
                     k.displayList();
      }
 }  

Inserting Element Between in Link List