Guest User

Untitled

a guest
Jun 24th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3. class node
  4. {
  5.     public int val;
  6.     public node link;
  7.     public void print()
  8.     {
  9.         System.out.print(val);
  10.     }
  11. };
  12. class linkedList
  13. {
  14.     private node first;
  15.     public linkedList()
  16.     {
  17.         first=null;
  18.     }
  19.     public boolean isEmpty()
  20.     {
  21.         return (first==null);
  22.     }
  23.     public void insert(int num)
  24.     {
  25.         node n=new node();
  26.         n.val=num;
  27.         if(isEmpty())
  28.         {
  29.             n.link=null;
  30.         }
  31.         else
  32.         {
  33.          n.link=first;
  34.         }
  35.         first=n;
  36.     }
  37.     public void pop(int num)
  38.     {
  39.         node n1=first.link,n2=first;
  40.            
  41.         while(n1.link!=null)
  42.         {
  43.             if(n1.val==num)
  44.             {
  45.                 n2.link=n1.link;
  46.                 System.out.print(n1.val+"has been removed from list");
  47.                 break;
  48.             }
  49.             n2=n2.link;
  50.             n1=n1.link;
  51.         }      
  52.     }
  53.     public void disp()
  54.     {
  55.         node n=first;
  56.         while(n.link!=null)
  57.         {
  58.             System.out.print(n.val+" ");
  59.         }
  60.     }
  61. };
  62. class program
  63. {
  64.     public static void main(String args[])
  65.     {  
  66.         linkedList l=new linkedList();
  67.         int choice,a,ch=1;
  68.         Scanner scan=new Scanner(System.in);
  69.         while(ch==1)
  70.         {
  71.        
  72.             System.out.print("1.inserting \n2.deleteing a purticular value \n3.displaying\n4.exit");
  73.             choice=scan.nextInt();
  74.             switch(choice)
  75.             {
  76.                 case 1:System.out.println("enter the number to be inserted");
  77.                        a=scan.nextInt();
  78.                        l.insert(a);
  79.                     break;
  80.                 case 2: System.out.println("enter the number to be deleted");
  81.                        a=scan.nextInt();
  82.                        l.delete(a);
  83.                     break;
  84.                 case 3:l.display();
  85.                     break;
  86.                  
  87.                 case 4:ch=0;
  88.                     break;
  89.             }
  90.         }
  91.     }
  92. }
Add Comment
Please, Sign In to add comment