Write About Creating Double linked list in Data Structures ?
https://www.computersprofessor.com/2016/11/write-about-creating-double-linked-list.html
In double linked list each element
is divided in to 3 parts.
A node in Double Linked List
Operations
performed on double linked lists :
1. Creation
2. Insertion
3. Deletion
4. Traversal
5. Searching
1. Creation :
class node
{
int n ;
node
prev, next;
}
node p,q,r,head = null, end = null;
System.out.println(“enter how many nodes do u want”);
int no=Integer.parseInt(br.readLine(
) );
while(no>=1)
{
p= new
node ( );
System.out.println(“enter
data”);
p.n= Integer.parseInt
(br.readLine( ));
p.prev=
null;
p.next=null;
if(head==null)
{
head=p;
end=p;
p=q;
}
else
{
q.next=p;
p.prev=q;
q=p;
end=p;
}
no --;
}
Traversal :In double linked lists we have two types of traversal.
1. Forward traversal
2. Backward traversal
1.
Forward traversal : visit or display each code
at least once using head.
p=head;
while(p!=null)
{
System.out.println(“data=”+p.n+”next address =”
+p.next);
p=p.next;
}
2. Backward traversal :
p=end;
while(p!=null)
{
System.out.println(“data=”+p.n+”prev address=”+p.prev);
p=p.prev;
}
Insertion : Insertion can be done at 3 places.
1. Insertion at beginning
2. Insertion at ending
3. Insertion at user choice
1. Insertion at beginning :
p=
new node;
System.out.println(“enter
data”);
p.n =
Integer.parseInt(br.readLine());
p.next=null;
p.prev=null;
p.next=head;
head.prev=p;
head=p;
2. Insertion at ending :
p=
new node;
System.out.println(“enter
data”);
p.n= Integer.parseInt
(br.readLine( ));
p.next=null;
p.prev=null;
end.next=p;
p.prev=end;
end=p;
3.Insertion at user choice :
p= new node;
System.out.println(“enter data”);
p.n= Integer.parseInt (br.readLine( ));
p.next = null;
p.prev = null;
System.out.println(“enter position”);
int pos= Integer.parseInt I(br.readLine( ));
q=head;
r=head.next;
while(pos – 1>1)
{
q=q.next;
r=r.next;
pos - -;
}
q.next =p;
p.prev=q;
p.next=r;
r.prev=p;
Deletion
from beginning :
head= head.
Next;
head.prev=null;
Deleting
at ending
:
end=end.prev;
end. next=null;
Deletion at user choice:
System.out.println(“enter position”);
pos= Integer.parseInt (br.readLine ());
q=head;
r=head. Next;
while(pos – 1>1)
{
q=q.next;
r=r.next;
pos --;
}
q.next=r.next;
r.next.prev=q;
searching :
int found =0;
p= head;
system.out.println(“enter element to search”);
int ele= Integer.parseInt (br.readLine());
while(p!==null)
{
if(p.n==ele)
{
found=1;
break;
}
p=p.next;
}
if(found==1)
{
System.out.println(“element
found”);
}
else
{
System.out.println(“element
not found”);
}
break;