Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- class node
- {
- public int val;
- public node link;
- public void print()
- {
- System.out.print(val);
- }
- };
- class linkedList
- {
- private node first;
- public linkedList()
- {
- first=null;
- }
- public boolean isEmpty()
- {
- return (first==null);
- }
- public void insert(int num)
- {
- node n=new node();
- n.val=num;
- if(isEmpty())
- {
- n.link=null;
- }
- else
- {
- n.link=first;
- }
- first=n;
- }
- public void pop(int num)
- {
- node n1=first.link,n2=first;
- while(n1.link!=null)
- {
- if(n1.val==num)
- {
- n2.link=n1.link;
- System.out.print(n1.val+"has been removed from list");
- break;
- }
- n2=n2.link;
- n1=n1.link;
- }
- }
- public void disp()
- {
- node n=first;
- while(n.link!=null)
- {
- System.out.print(n.val+" ");
- }
- }
- };
- class program
- {
- public static void main(String args[])
- {
- linkedList l=new linkedList();
- int choice,a,ch=1;
- Scanner scan=new Scanner(System.in);
- while(ch==1)
- {
- System.out.print("1.inserting \n2.deleteing a purticular value \n3.displaying\n4.exit");
- choice=scan.nextInt();
- switch(choice)
- {
- case 1:System.out.println("enter the number to be inserted");
- a=scan.nextInt();
- l.insert(a);
- break;
- case 2: System.out.println("enter the number to be deleted");
- a=scan.nextInt();
- l.delete(a);
- break;
- case 3:l.display();
- break;
- case 4:ch=0;
- break;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment