Write About Creating Double linked list in Data Structures ?


     In double linked list each element is divided in to 3 parts.

 A node in Double Linked List

node


Operations performed on double linked lists :

1.    Creation
2.    Insertion
3.    Deletion
4.    Traversal
5.    Searching

   1.    Creation :

example for dll


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.

forward traversing

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 :

insertion

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 :

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 :

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 :

deletion at begining


            head= head. Next;
            head.prev=null;

Deleting at ending :

deletion at ending

            end=end.prev;
            end. next=null;

Deletion at user choice:

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;



Related

Data Structures 2350943010221425532

Post a Comment

emo-but-icon

item