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 delmid(int x)
                                        {
                                             p=first;
                                             q=first.nxt;
                                             while(q !=null)
                                                    {
                                                            if(q.v==x)
                                                              {
                                                                  p.nxt=q.nxt;
                                                                  q=null;
                                                                   break;
                                                               }
                                                                    else
                                                                        {
                                                                            p=q;
                                                                            q=q.nxt;
                                                                         }
                                                    }
                                        }
                                          public void displayList()
                                             {
                                                  node r=first;
                                                  while(r !=null)
                                                          {
                                                              r.dispval();
                                                              r=r.nxt;
                                                          }
                                             }
          }
      class DeletingDesireElement
      {
          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 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 delete");
                            h=b.readLine();
                            m=Integer.parseInt(h);
                            k.delmid(m);
                            System.out.println("Data in Linked List after deleting desired element is");
                            k.displayList();
           }
     }                                                                             

Deleting Desire Element of Linked List