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 insbeg(int x)
            {
                 node p=new node(x);
                 p.nxt=first;
                 first=p;
            }
                 public void displayList()
                  {
                       node r=first;
                       while(r !=null)
                             {
                                 r.dispval();
                                 r=r.nxt;
                             }
                  }
  }
 class InsertElementLinkedList
 {
       public static void main(String args[]) throws IOException
         {
            LinkList k=new LinkList();
            BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
            String h;
            int i,m;
            for(i=1;i<=5;i++)
                {
                 k.insertval(i);
                }
                   System.out.println("Data in Linked list is");
                   k.displayList();
                   System.out.println("enter the numerical value to insert");
                   h=b.readLine();
                   m=Integer.parseInt(h);
                   k.insbeg(m);
                   System.out.println("Data in linked list after inserting an element in beginning is");
                   k.displayList();
        }
 }

Insert Element in Linked List