import java.io.*;
class node
{ int data;
node next;
node prev;
node(int d)
{ data=d; }
}
class link
{ private node first;
private node last;
public link()
{ first=null; last=null; }
public void insfirst(int a)
{ node newnode=new node(a);
newnode.next=first;
first=newnode;
}
public void delfirst()
{ if(first==null)
{ System.out.println("UNDERFLOW"); }
else
{ first=first.next; }
}
public void insafter(int b,int c)
{ node ptr; ptr=first; node newnode=new node(c);
while(ptr.next.data!=b)
{ ptr=ptr.next; }
if(ptr.data==b)
{ newnode.next=ptr.next;
newnode.prev=ptr;
ptr.next.prev=newnode;
ptr.next=newnode;
}
else
{ System.out.println("No. is not found in the list"); }
}
public void delafter(int b)
{ node ptr=first;
if(first==null)
{ System.out.println("Underflow"); }
else
{ while(ptr.data!=b)
ptr=ptr.next;
if(ptr.next.data==b)
{ ptr.next.next.prev=ptr;
ptr.next=ptr.next.next;
}
else
{ System.out.println("Data is not found"); }
}
}
public void display()
{ node ptr; ptr=first;
while(ptr!=null)
{ System.out.println(+ptr.data); ptr=ptr.next; }
}
}
class doublylinkedlist
{
public static void main(String args[])throws IOException
{ int x; link mm=new link();
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
do
{ System.out.println("*********DOUBLY LINKED LIST::OPTIONS********");
System.out.println("1.>Insert first\n2.>Delete first\n3.>Insert after\n4.>Delete after\n5.>Display\n6.>Exit");
System.out.println("Enter your option here::");
x=Integer.parseInt(br.readLine());
switch(x)
{ case 1: System.out.println("Enter the node data");
int y=Integer.parseInt(br.readLine());
mm.insfirst(y);
break;
case 2: mm.delfirst(); break;
case 3: System.out.println("Enter the position value and new node value");
int l=Integer.parseInt(br.readLine());
int m=Integer.parseInt(br.readLine());
mm.insafter(l,m); break;
case 4: System.out.println("Enter the value to be deleted");
int n=Integer.parseInt(br.readLine());
mm.delafter(n); break;
case 5: mm.display(); break;
case 6: System.out.println("By"); break;
default: System.out.println("Try again");
}
}
while(x!=6);
}
catch(Exception e){ System.out.println("---->"+e);}
}
}
class node
{ int data;
node next;
node prev;
node(int d)
{ data=d; }
}
class link
{ private node first;
private node last;
public link()
{ first=null; last=null; }
public void insfirst(int a)
{ node newnode=new node(a);
newnode.next=first;
first=newnode;
}
public void delfirst()
{ if(first==null)
{ System.out.println("UNDERFLOW"); }
else
{ first=first.next; }
}
public void insafter(int b,int c)
{ node ptr; ptr=first; node newnode=new node(c);
while(ptr.next.data!=b)
{ ptr=ptr.next; }
if(ptr.data==b)
{ newnode.next=ptr.next;
newnode.prev=ptr;
ptr.next.prev=newnode;
ptr.next=newnode;
}
else
{ System.out.println("No. is not found in the list"); }
}
public void delafter(int b)
{ node ptr=first;
if(first==null)
{ System.out.println("Underflow"); }
else
{ while(ptr.data!=b)
ptr=ptr.next;
if(ptr.next.data==b)
{ ptr.next.next.prev=ptr;
ptr.next=ptr.next.next;
}
else
{ System.out.println("Data is not found"); }
}
}
public void display()
{ node ptr; ptr=first;
while(ptr!=null)
{ System.out.println(+ptr.data); ptr=ptr.next; }
}
}
class doublylinkedlist
{
public static void main(String args[])throws IOException
{ int x; link mm=new link();
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
do
{ System.out.println("*********DOUBLY LINKED LIST::OPTIONS********");
System.out.println("1.>Insert first\n2.>Delete first\n3.>Insert after\n4.>Delete after\n5.>Display\n6.>Exit");
System.out.println("Enter your option here::");
x=Integer.parseInt(br.readLine());
switch(x)
{ case 1: System.out.println("Enter the node data");
int y=Integer.parseInt(br.readLine());
mm.insfirst(y);
break;
case 2: mm.delfirst(); break;
case 3: System.out.println("Enter the position value and new node value");
int l=Integer.parseInt(br.readLine());
int m=Integer.parseInt(br.readLine());
mm.insafter(l,m); break;
case 4: System.out.println("Enter the value to be deleted");
int n=Integer.parseInt(br.readLine());
mm.delafter(n); break;
case 5: mm.display(); break;
case 6: System.out.println("By"); break;
default: System.out.println("Try again");
}
}
while(x!=6);
}
catch(Exception e){ System.out.println("---->"+e);}
}
}
No comments:
Post a Comment