Doubly Linked List

In Doubly linked list, we can store in each node not only the address of next node but also the address of the previous node in the linked list.

 A node in doubly linked list has 3 fields……..

                1)Data

                2)Left Link

                3)Right Link

 

 

Implementation of doubly linked list

                Struct node

                  {

                     Int data;

                    Struct node *llink;

                    Struct node *rlink;

                 }

/* Program of double linked list*/

# include <stdio.h>

# include <malloc.h>

#include<conio.h>

struct nodd

{

                struct nodd *pree;

                int infoo;

                struct nodd *nexxt;

}*start;

void main()

{

                int choicc,n,m,po,ii;

                start=NULL;

                while(1)

                {

                                printf("1.Create List....\n");

                                printf("2.Add at begining....\n");

                                printf("3.Add after....\n");

                                printf("4.Delete....\n");

                                printf("5.Display....\n");

                                printf("6.Count....\n");

                                printf("7.Reverse....\n");

                                printf("8.exit....\n");

                                printf("Pls Enter ur choicce.... : ");

                                scanf("%d",&choicc);

                                switch(choicc)

                                {

                                 case 1:

                                                printf("How many noddes you want : ");

                                                scanf("%d",&n);

                                                for(ii=0;ii<n;ii++)

                                                {

                                                                printf("Enter the element : ");

                                                                scanf("%d",&m);

                                                                createe_list(m);

                                                }

                                                break;

                                 case 2:

                                                printf("Enter the element : ");

                                                scanf("%d",&m);

                                                addatbegn(m);

                                                break;

                                 case 3:

                                                printf("Enter the element : ");

                                                scanf("%d",&m);

                                                printf("Pls Enter the position after(1,2,3...) which this element is inserted : ");

                                                scanf("%d",&po);

                                                addafteer(m,po);

                                                break;

                                 case 4:

                                                printf("Please!Enter the element for deletion.... : ");

                                                scanf("%d",&m);

                                                dell(m);

                                                break;

                                 case 5:

                                                displaay();

                                                break;

                                 case 6:

                                                couunt();

                                                break;

                                 case 7:

                                                revs();

                                                break;

                                 case 8:

                                                exit();

                                 default:

                                                printf("Wrong choice\n");

                }

   }

}

 createe_list(int num)

{

                struct nodd *q,*tmp;

                tmp= malloc(sizeof(struct nodd));

                tmp->infoo=num;

                tmp->nexxt=NULL;

                if(start==NULL)

                {

                                tmp->pree=NULL;

                                start->pree=tmp;

                                start=tmp;

                }

                else

                {

                                q=start;

                                while(q->nexxt!=NULL)

                                                q=q->nexxt;

                                q->nexxt=tmp;

                                tmp->pree=q;

                }

}

 addatbegn(int num)

{

                struct nodd *tmp;

                tmp=malloc(sizeof(struct nodd));

                tmp->pree=NULL;

                tmp->infoo=num;

                tmp->nexxt=start;

                start->pree=tmp;

                start=tmp;

}/*End of addatbegn()*/

addafteer(int num,int c)

{

                struct nodd *tmp,*q;

                int ii;

                q=start;

                for(ii=0;ii<c-1;ii++)

                {

                                q=q->nexxt;

                                if(q==NULL)

                                {

                                                printf("There are less than %d elements\n",c);

                                                return;

                                }

                }

                tmp=malloc(sizeof(struct nodd) );

                tmp->infoo=num;

                q->nexxt->pree=tmp;

                tmp->nexxt=q->nexxt;

                tmp->pree=q;

                q->nexxt=tmp;

}/*End of addafteer() */

dell(int num)

{

                struct nodd *tmp,*q;

                if(start->infoo==num)

                {

                                tmp=start;

                                start=start->nexxt;  /*oh first element deleted*/

                                start->pree = NULL;

                                free(tmp);

                                return;

                }

                q=start;

                while(q->nexxt->nexxt!=NULL)

                {

                                if(q->nexxt->infoo==num)

                                {

                                                tmp=q->nexxt;

                                                q->nexxt=tmp->nexxt;

                                                tmp->nexxt->pree=q;

                                                free(tmp);

                                                return;

                                }

                                q=q->nexxt;

                }

                if(q->nexxt->infoo==num)    /*oh last element deleted*/

                {              tmp=q->nexxt;

                                free(tmp);

                                q->nexxt=NULL;

                                return;

                }

                printf("Oh!Element which u entered  %d not found\n",num);

}

displaay()

{

                struct nodd *q;

                if(start==NULL)

                {

                                printf("Oh!List is empty\n");

                                return;

                }

                q=start;

                printf("List is :\n");

                while(q!=NULL)

                {

                                printf("%d ", q->infoo);

                                q=q->nexxt;

                }

                printf("\n");

}

couunt()

{              struct nodd *q=start;

                int cunt=0;

                while(q!=NULL)

                {

                                q=q->nexxt;

                                cunt++;

                }

                printf("Number of elements are %d=\n",cunt);

}

revs()

{

                struct nodd *p1,*p2;

                p1=start;

                p2=p1->nexxt;

                p1->nexxt=NULL;

                p1->pree=p2;

                while(p2!=NULL)

                {

                                p2->pree=p2->nexxt;

                                p2->nexxt=p1;

                                p1=p2;

                                p2=p2->pree; /*nexxt of p2 changed to pree */

                }

                start=p1;

}/*End of revs()*/

Double Linked List