import java.io.*; class lists { int data; lists next; } class slist { lists head,last,new1,temp; slist() { head=null; } public void create(int x) { new1=new lists(); if(new1==null) { System.out.println("creation is not poosible"); return; } new1.data=x; if(head==null) { head=last=new1; last.next=null; } else { last.next=new1; last=new1; last.next=null; } } public void display() { if(head==null) { System.out.println("Empty list"); return; } System.out.println("The Elements are:"); for(temp=head;temp!=null;temp=temp.next) { System.out.print(temp.data+"\t"); } System.out.println(); } public void insert(int x,int d) { int pos,i; pos=x; new1=new lists(); if(new1==null) { System.out.println("Insufficient Memory"); return; } new1.data=d; if(head==null && pos==1) { head=last=new1; new1.next=null; } else if(head!=null && pos==1) { new1.next=head; head=new1; } else { i=2; temp=head; while(i<pos) { temp=temp.next; i++; } if(temp==null) { last.next=new1; last=new1; last.next=null; } else { new1.next=temp.next; temp.next=new1; } } } public void deletes(int p) { int pos,i; lists curr; pos=p; if(head==null) { System.out.println("Empty Linked List"); return; } if(pos==1) { temp=head; head=head.next; temp=null; } else { i=2; temp=head; curr=temp.next; while(i<pos) { temp=temp.next; curr=temp.next; i++; } temp.next=curr.next; curr=null; } } } class linklist { public static void main(String []args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); slist sl=new slist(); int opt,y,z; while(true) { System.out.println("1.creation"); System.out.println("2.Insertion"); System.out.println("3.Deletion"); System.out.println("4.display"); System.out.println("5.exit"); System.out.println("enter ur choice:"); opt=Integer.parseInt(br.readLine()); switch(opt) { case 1: System.out.println("Enter the number:"); y=Integer.parseInt(br.readLine()); sl.create(y); break; case 2: System.out.println("Enter the position:"); y=Integer.parseInt(br.readLine()); System.out.println("Enter the Data:"); z=Integer.parseInt(br.readLine()); sl.insert(y,z); break; case 3: System.out.println("Enter the position:"); y=Integer.parseInt(br.readLine()); sl.deletes(y); break; case 4: sl.display(); break; case 5: System.exit(0); } } } }
Java program to demonstrate Linkedlist.
Reviewed by
on
December 22, 2019
Rating:

No comments: