Program to
implement Doubly Linked List
import
java.io.*;
import
java.lang.*;
import
java.util.*;
class
Nodetype
{
int info;
Nodetype right,left;
Nodetype(int i)
{
info=i;
right=null;
left=null;
}}
class
Operations
{
Nodetype list=null;
void display()
{
/*display all nodes*/
Nodetype t;
t=list;
if(t==null)
System.out.println("\nThe linked list is empty");
else
{
System.out.print("\nList");
while(t!=null)
{
System.out.print("<==>|"+t.info+"|");
t=t.right;
}
}
}
/*end display*/
void insertbeg(int x)
{
/*insert new element in the beginning of linked list*/
Nodetype q=new Nodetype(x);
q.info=x;
q.right=list;
list=q;
display();
}
/*end insertbeg*/
void deletebeg()
{
/*delete a node from the beginning of the linked list*/
int x;
Nodetype q;
q=list;
if(list==null)
System.out.println("\nThe linked list is empty");
else
{
x=q.info;
list=q.right;
(q.right).left=null;
System.out.println("\n\nThe deleted element is "+x);
display();
}
}
/*end deletebeg*/
void insertend(int x)
{
/*insert new element at the end of linked list*/
Nodetype temp;
Nodetype q=new Nodetype(x);
q.info=x;
q.right=null;
temp=list;
if(list==null)
{
list=q;
q.left=null;
}
else
{
while(temp.right!=null)
temp=temp.right;
temp.right=q;
q.left=temp;
}
display();
}
/*end insertend*/
void deleteend()
{
/*delete a node from the end of the linked list*/
Nodetype q,temp=null;
int x;
q=list;
if(list==null)
System.out.println("\nThe linked list is empty");
else
{
if(list.right==null)
list=null;
else
{
while(q.right!=null)
{
temp=q;
q=q.right;
}
temp.right=null;
}
System.out.println("\n\nThe deleted element is "+q.info);
}
display();
}
/*end deleteend
void
insright(int x,int nx)
{
/*insert new element towards right of a specific node*/
Nodetype r,t;
t=list;
do
{
if(t.info==x) /*node
found*/
break;
else
t=t.right;
}while(t!=null);
if(t==null)
{
System.out.println("\nElement not found");
return;
}
Nodetype q=new Nodetype(x);
q.info=nx;
r=t.right;
if(r!=null)
r.left=q;
q.right=r;
q.left=t;
t.right=q;
display();
}
/*end insright*/
void
delright(int x)
{
/*delete a node which is towards right of a node*/
Nodetype q,t;
t=list;
do
{
if(t.info==x) /*node found*/
break;
else
t=t.right;
} while(t!=null);
if(t==null)
{
System.out.println("\nElement not found");
return;
}
if(t.right==null)
{
System.out.println("\nThere is no right node");
return;
}
q=t.right;
System.out.println("\n\nThe deleted element is "+q.info);
t.right=q.right;
if(q.right!=null)
(q.right).left=t;
display();
}
/*end delright*/
void
insleft(int x,int nx)
{
/*insert new node towards left of a specific node*/
Nodetype l,t;
t=list;
do
{
if(t.info==x)
break;
else
t=t.right;
} while(t!=null);
if(t==null)
{
System.out.println("\nElement not found");
return;
}
Nodetype q=new Nodetype(x);
q.info=nx;
l=t.left;
if(l!=null)
l.right=q;
q.right=t;
q.left=l;
t.left=q;
if(t==list)
list=q;
display();
}
/*end insleft*/
void
delleft(int x)
{
/*delete a node which is towards left of a specific node*/
Nodetype l,q,t;
t=list;
do
{
if(t.info==x)
break;
else
t=t.right;
}
while(t!=null);
if(t==null)
{
System.out.println("\nElement not found");
return;
}
if(t.left==null)
{
System.out.println("\nThere is no left node");
return;
}
q=t.left;
if(q==list)
list=t;
System.out.println("\n\nThe deleted element is "+q.info);
t.left=q.left;
l=q.left;
if(l!=null)
(q.left).right=t;
display();
}
/*end delleft*/
}
class
LLDoubly
{
public static void main(String args[])
{
Operations L =new Operations();
System.out.print("\nInsert 55,50,40,90 in the begining ");
L.insertbeg(55);
L.insertbeg(50);
L.insertbeg(40);
L.insertbeg(90);
System.out.print("\n\nDelete 3 items from the beginning");
L.deletebeg();
L.deletebeg();
L.deletebeg();
System.out.print("\n\nInsert 11,22,33,44 in the end ");
L.insertend(11);
L.insertend(22);
L.insertend(33);
L.insertend(44);
System.out.print("\n\nDelete 2 items from the end");
L.deleteend();
L.deleteend();
System.out.print("\n\nInsert 100 to right of 11");
L.insright(11,100);
System.out.print("\n\nInsert 80 to left of 44");
L.insleft(44,80);
System.out.print("\n\nInsert 81 to left of 55");
L.insleft(55,81);
System.out.print("\n\nDelete item right of 100");
L.delright(100);
System.out.print("\n\nDelete item left of 22");
L.delleft(2);
System.out.print("\n\nDelete item left of 11");
L.delleft(11);
} /*end main*/
}
Output:
Insert
55,50,40,90 in the begining
List<==>|55|
List<==>|50|<==>|55|
List<==>|40|<==>|50|<==>|55|
List<==>|90|<==>|40|<==>|50|<==>|55|
Delete 3
items from the beginning
The deleted
element is 90
List<==>|40|<==>|50|<==>|55|
The deleted
element is 40
List<==>|50|<==>|55|
The deleted
element is 50
List<==>|55|
Insert
11,22,33,44 in the end
List<==>|55|<==>|11|
List<==>|55|<==>|11|<==>|22|
List<==>|55|<==>|11|<==>|22|<==>|33|
List<==>|55|<==>|11|<==>|22|<==>|33|<==>|44|
Delete 2
items from the end
The deleted
element is 44
List<==>|55|<==>|11|<==>|22|<==>|33|
The deleted
element is 33
List<==>|55|<==>|11|<==>|22|
Insert 100
to right of 11
List<==>|55|<==>|11|<==>|100|<==>|22|
Insert 80 to
left of 44
Element not
found
Insert 81 to
left of 55
List<==>|81|<==>|55|<==>|11|<==>|100|<==>|22|
Delete item
right of 100
The deleted
element is 22
List<==>|81|<==>|55|<==>|11|<==>|100|
Delete item
left of 22
Element not
found
Delete item
left of 11
The deleted
element is 55
List<==>|81|<==>|11|<==>|100|
No comments:
Post a Comment