import java.io.*;
class dlink
{
int data;
dlink prev,next;
}
class dlinks
{
dlink head,temp,new1,last,curr;
void create(int x)
{
new1=new dlink();
if(new1==null)
{
System.out.println("Insufficient Memory");
System.exit(0);
}
else
{
new1.data=x;
if(head==null)
{
head=last=new1;
head.prev=null;
head.next=null;
}
else
{
last.next=new1;
new1.prev=last;
last=new1;
last.next=null;
}
}
}
void displayfor()
{
if(head==null)
{
System.out.println("Empty Linked List");
return;
}
for(temp=head;temp!=null;temp=temp.next)
{
System.out.println(temp.data);
}
System.out.println(" ");
}
void displayback()
{
if(head==null)
{
System.out.println("Empty Linked List");
return;
}
for(temp=last;temp!=null;temp=temp.prev)
{
System.out.println(temp.data);
}
System.out.println(" ");
}
void insert(int p,int d)
{
int pos,i;
new1=new dlink();
pos=p;
new1.data=d;
if(head==null && pos==1)
{
head=last=new1;
head.prev=null;
head.next=null;
}
else if(head!=null & pos==1)
{
new1.next=head;
head.prev=new1;
head=new1;
head.prev=null;
}
else
{
i=2;
temp=head;
while(i<pos)
{
temp=temp.next;
i++;
}
if(temp.next==null)
{
last.next=new1;
new1.prev=last;
last=new1;
last.next=null;
}
else
{
new1.next=temp.next;
temp.next.prev=new1;
temp.next=new1;
new1.prev=temp;
}
}
}
void deletes(int p)
{
int pos,i;
pos=p;
if(head==null)
{
System.out.println("Empty Linked List");
return;
}
if(pos==1)
{
if(head==last)
{
temp=head;
head=head.next;
last=null;
temp=null;
}
else
{
temp=head;
head=head.next;
head.prev=null;
temp=null;
}
}
else
{
i=2;
temp=head;
curr=temp.next;
while(i<pos)
{
i++;
temp=temp.next;
curr=temp.next;
}
if(curr.next==null)
{
temp.next=curr.next;
last=last.prev;
curr=null;
}
else
{
temp.next=curr.next;
curr.next.prev=temp;
curr=null;
}
}
}
}
class doublelink
{
public static void main(String []args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
dlinks dl=new dlinks();
int ch,z,y;
while(true)
{
System.out.println("1.Creation");
System.out.println("2.Insertion");
System.out.println("3.Deletion");
System.out.println("4.Display Forward");
System.out.println("5.Display Backward");
System.out.println("6.Exit");
System.out.println("Enter the choice:");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1: System.out.println("Enter the number:");
z=Integer.parseInt(br.readLine());
dl.create(z);
break;
case 2: System.out.println("Enter the position:");
y=Integer.parseInt(br.readLine());
System.out.println("Enter the number:");
z=Integer.parseInt(br.readLine());
dl.insert(y,z);
break;
case 3: System.out.println("Enter the position:");
y=Integer.parseInt(br.readLine());
dl.deletes(y);
break;
case 4: dl.displayfor();
break;
case 5: dl.displayback();
break;
case 6: System.exit(0);
}
}
} }
class dlink
{
int data;
dlink prev,next;
}
class dlinks
{
dlink head,temp,new1,last,curr;
void create(int x)
{
new1=new dlink();
if(new1==null)
{
System.out.println("Insufficient Memory");
System.exit(0);
}
else
{
new1.data=x;
if(head==null)
{
head=last=new1;
head.prev=null;
head.next=null;
}
else
{
last.next=new1;
new1.prev=last;
last=new1;
last.next=null;
}
}
}
void displayfor()
{
if(head==null)
{
System.out.println("Empty Linked List");
return;
}
for(temp=head;temp!=null;temp=temp.next)
{
System.out.println(temp.data);
}
System.out.println(" ");
}
void displayback()
{
if(head==null)
{
System.out.println("Empty Linked List");
return;
}
for(temp=last;temp!=null;temp=temp.prev)
{
System.out.println(temp.data);
}
System.out.println(" ");
}
void insert(int p,int d)
{
int pos,i;
new1=new dlink();
pos=p;
new1.data=d;
if(head==null && pos==1)
{
head=last=new1;
head.prev=null;
head.next=null;
}
else if(head!=null & pos==1)
{
new1.next=head;
head.prev=new1;
head=new1;
head.prev=null;
}
else
{
i=2;
temp=head;
while(i<pos)
{
temp=temp.next;
i++;
}
if(temp.next==null)
{
last.next=new1;
new1.prev=last;
last=new1;
last.next=null;
}
else
{
new1.next=temp.next;
temp.next.prev=new1;
temp.next=new1;
new1.prev=temp;
}
}
}
void deletes(int p)
{
int pos,i;
pos=p;
if(head==null)
{
System.out.println("Empty Linked List");
return;
}
if(pos==1)
{
if(head==last)
{
temp=head;
head=head.next;
last=null;
temp=null;
}
else
{
temp=head;
head=head.next;
head.prev=null;
temp=null;
}
}
else
{
i=2;
temp=head;
curr=temp.next;
while(i<pos)
{
i++;
temp=temp.next;
curr=temp.next;
}
if(curr.next==null)
{
temp.next=curr.next;
last=last.prev;
curr=null;
}
else
{
temp.next=curr.next;
curr.next.prev=temp;
curr=null;
}
}
}
}
class doublelink
{
public static void main(String []args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
dlinks dl=new dlinks();
int ch,z,y;
while(true)
{
System.out.println("1.Creation");
System.out.println("2.Insertion");
System.out.println("3.Deletion");
System.out.println("4.Display Forward");
System.out.println("5.Display Backward");
System.out.println("6.Exit");
System.out.println("Enter the choice:");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1: System.out.println("Enter the number:");
z=Integer.parseInt(br.readLine());
dl.create(z);
break;
case 2: System.out.println("Enter the position:");
y=Integer.parseInt(br.readLine());
System.out.println("Enter the number:");
z=Integer.parseInt(br.readLine());
dl.insert(y,z);
break;
case 3: System.out.println("Enter the position:");
y=Integer.parseInt(br.readLine());
dl.deletes(y);
break;
case 4: dl.displayfor();
break;
case 5: dl.displayback();
break;
case 6: System.exit(0);
}
}
} }
No comments:
Post a Comment