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 insend(int x)
         {
              node p=new node(x);
              p.nxt=null;
              q=first;
               while(q.nxt !=null)
                    {
                        q=q.nxt;
                    }
                       q.nxt=p;
         }
             public void displayList()
             {
                 node r= first;
                while(r !=null)
                      {
                         r.dispval();
                         r=r.nxt;
                      }
            }
}
  class InsertElementEnd
  {
       public static void main(String args[]) throws IOException
              {
                   LinkList k = new LinkList();
                   BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
                   String h;
                   int n,i,m;
                   System.out.println("how many numbers you want to insert in linked list");
                   h=b.readLine();
                   n=Integer.parseInt(h);
                   System.out.println("Enter numricals 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 value to add at the end of the link list");
                           h=b.readLine();
                           m=Integer.parseInt(h);
                           k.insend(m);
                           System.out.println("Data in linked list after adding the element at the end is");
                           k.displayList();
            }
 }                                                                                   

Insert Element at End in Linked List