C Program of Double Linked List

#include <stdio.h>
#include <malloc.h>

struct node
{
	struct node *prev;
	int info;
	struct node *next;
}*start;

main()
{
	int choice,n,m,po,i;
	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("Enter your choice : ");
		scanf("%d",&choice);
		switch(choice)
		{
		 case 1:
			printf("How many nodes you want : ");
			scanf("%d",&n);
			for(i=0;i<n;i++)
			{
				printf("Enter the element : ");
				scanf("%d",&m);
				create_list(m);
			}
			break;
		 case 2:
			printf("Enter the element : ");
			scanf("%d",&m);
			addatbeg(m);
			break;
		 case 3:
			printf("Enter the element : ");
			scanf("%d",&m);
			printf("Enter the position after which this element is inserted : ");
			scanf("%d",&po);
			addafter(m,po);
			break;
		 case 4:
			printf("Enter the element for deletion : ");
			scanf("%d",&m);
			del(m);
			break;
		 case 5:
			display();
			break;
		 case 6:
			count();
			break;
		 case 7:
			rev();
			break;
		 case 8:
			exit();
		 default:
			printf("Wrong choice\n");
	}/*End of switch*/
   }/*End of while*/
}/*End of main()*/

create_list(int num)
{
	struct node *q,*tmp;
	tmp= malloc(sizeof(struct node));
	tmp->info=num;
	tmp->next=NULL;
	if(start==NULL)
	{
		tmp->prev=NULL;
		start->prev=tmp;
		start=tmp;
	}
	else
	{
		q=start;
		while(q->next!=NULL)
			q=q->next;
		q->next=tmp;
		tmp->prev=q;
	}
}/*End of create_list()*/

addatbeg(int num)
{
	struct node *tmp;
	tmp=malloc(sizeof(struct node));
	tmp->prev=NULL;
	tmp->info=num;
	tmp->next=start;
	start->prev=tmp;
	start=tmp;
}/*End of addatbeg()*/

addafter(int num,int c)
{
	struct node *tmp,*q;
	int i;
	q=start;
	for(i=0;i<c-1;i++)
	{
		q=q->next;
		if(q==NULL)
		{
			printf("There are less than %d elements\n",c);
			return;
		}
	}
	tmp=malloc(sizeof(struct node) );
	tmp->info=num;
	q->next->prev=tmp;
	tmp->next=q->next;
	tmp->prev=q;
	q->next=tmp;
}/*End of addafter() */

del(int num)
{
	struct node *tmp,*q;
	if(start->info==num)
	{
		tmp=start;
		start=start->next;  /*first element deleted*/
		start->prev = NULL;
		free(tmp);
		return;
	}
	q=start;
	while(q->next->next!=NULL)
	{
		if(q->next->info==num)     /*Element deleted in between*/
		{
			tmp=q->next;
			q->next=tmp->next;
			tmp->next->prev=q;
			free(tmp);
			return;
		}
		q=q->next;
	}
	if(q->next->info==num)    /*last element deleted*/
	{ 	tmp=q->next;
		free(tmp);
		q->next=NULL;
		return;
	}
	printf("Element %d not found\n",num);
}/*End of del()*/

display()
{
	struct node *q;
	if(start==NULL)
	{
		printf("List is empty\n");
		return;
	}
	q=start;
	printf("List is :\n");
	while(q!=NULL)
	{
		printf("%d ", q->info);
		q=q->next;
	}
	printf("\n");
}/*End of display() */

count()
{ 	struct node *q=start;
	int cnt=0;
	while(q!=NULL)
	{
		q=q->next;
		cnt++;
	}
	printf("Number of elements are %d\n",cnt);
}/*End of count()*/

rev()
{
	struct node *p1,*p2;
	p1=start;
	p2=p1->next;
	p1->next=NULL;
	p1->prev=p2;
	while(p2!=NULL)
	{
		p2->prev=p2->next;
		p2->next=p1;
		p1=p2;
		p2=p2->prev; /*next of p2 changed to prev */
	}
	start=p1;
}/*End of rev()*/
VN:F [1.9.22_1171]
Rating: 8.3/10 (240 votes cast)
VN:F [1.9.22_1171]
Rating: +120 (from 160 votes)
C Program of Double Linked List, 8.3 out of 10 based on 240 ratings

19. June 2010 by Jishnu
Categories: C Programming, Data Structures | Tags: | 25 comments

Comments (25)

  1. lovaly

  2. while running this code in my system,there is segmentation fault occurs at second scanf() statement in case:1.
    why it is coming I am not understanding?

  3. check whether you copied correctly.

  4. // Below program is running successfully on any compiler

    // Remove system(“PAUSE”); if you are not using DEV C/C++ for ex.. on gcc compiler this statement is not required.

    #include
    #include

    struct node
    {
    struct node *prev;
    int info;
    struct node *next;
    };
    struct node *start = NULL;
    void create_list(int num)
    {
    struct node *q,*tmp;
    tmp= (struct node *) malloc(sizeof(struct node));

    tmp->info=num;
    tmp->next=NULL;
    if(start==NULL)
    {
    printf(“Creating the first node of Linked List \n”);
    tmp->prev=NULL;
    //start->prev=tmp;
    start=tmp;
    }
    else
    {
    printf(“Inserting the nodes after the head node/other nodes\n”);
    q=start;
    while(q->next!=NULL)
    q=q->next;
    q->next=tmp;
    tmp->prev=q;
    }
    }/*End of create_list()*/

    void addatbeg(int num)
    {
    struct node *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->prev=NULL;
    tmp->info=num;
    tmp->next=start;
    start->prev=tmp;
    start=tmp;
    }/*End of addatbeg()*/

    void addafter(int num,int c)
    {
    struct node *tmp,*q;
    int i;
    q=start;
    for(i=0;inext;
    if(q==NULL)
    {
    printf(“There are less than %d elements\n”,c);
    return;
    }
    }
    tmp=(struct node *)malloc(sizeof(struct node) );
    tmp->info=num;
    if(q->next == NULL)
    {
    //Insertion at end
    q->next = tmp;
    tmp->prev = q;
    tmp->next = NULL;
    return;
    }
    q->next->prev=tmp;
    tmp->next=q->next;
    tmp->prev=q;
    q->next=tmp;
    }/*End of addafter() */

    void del(int num)
    {
    struct node *tmp,*q;
    if(start == NULL)
    {
    printf(“List is empty \n”);
    return;
    }
    if((start->next == NULL)&&(start->info!= num))
    {
    printf(“Element %d not found\n”,num);
    return;
    }
    if(start->info==num)
    {
    printf(“First element deleted \n”);
    tmp=start;
    if(start->next !=NULL)
    {
    start=start->next; /*first element deleted*/
    start->prev = NULL;
    free(tmp);
    }
    else
    {
    start->next = NULL;
    start->prev = NULL;
    free(start);
    start = NULL;
    }

    // free(tmp);
    return;
    }
    q=start;
    while(q->next->next!=NULL)
    {
    if(q->next->info==num) /*Element deleted in between*/
    {
    tmp=q->next;
    q->next=tmp->next;
    tmp->next->prev=q;
    free(tmp);
    return;
    }
    q=q->next;
    }

    if(q->next->info==num) /*last element deleted*/
    {

    tmp=q->next;
    free(tmp);
    q->next=NULL;
    return;
    }
    printf(“Element %d not found\n”,num);
    }/*End of del()*/

    void display()
    {
    struct node *q;
    if(start==NULL)
    {
    printf(“List is empty\n”);
    return;
    }
    q=start;
    printf(“List is :\n”);
    while(q!=NULL)
    {
    printf(“%d “, q->info);
    q=q->next;
    }
    printf(“\n”);
    }/*End of display() */

    void count()
    { struct node *q=start;
    int cnt=0;
    while(q!=NULL)
    {
    q=q->next;
    cnt++;
    }
    printf(“Number of elements are %d\n”,cnt);
    }/*End of count()*/

    void rev()
    {
    struct node *p1,*p2;
    p1=start;
    p2=p1->next;
    p1->next=NULL;
    p1->prev=p2;
    while(p2!=NULL)
    {
    p2->prev=p2->next;
    p2->next=p1;
    p1=p2;
    p2=p2->prev; /*next of p2 changed to prev */
    }
    start=p1;
    }/*End of rev()*/

    main()
    {
    int choice,n,m,po,i;
    //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(“Enter your choice : “);
    scanf(“%d”,&choice);
    switch(choice)
    {
    case 1:
    printf(“How many nodes you want : “);
    scanf(“%d”,&n);
    for(i=0;i<n;i++)
    {
    printf("Enter the element : ");
    scanf("%d",&m);
    create_list(m);
    }
    break;
    case 2:
    printf("Enter the element : ");
    scanf("%d",&m);
    addatbeg(m);
    break;
    case 3:
    printf("Enter the element : ");
    scanf("%d",&m);
    printf("Enter the position after which this element is inserted : ");
    scanf("%d",&po);
    addafter(m,po);
    break;
    case 4:
    printf("Enter the element for deletion : ");
    scanf("%d",&m);
    del(m);
    break;
    case 5:
    display();
    break;
    case 6:
    count();
    break;
    case 7:
    rev();
    break;
    case 8:
    exit(0);
    default:
    printf("Wrong choice\n");
    }/*End of switch*/
    system("PAUSE");
    }/*End of while*/
    }/*End of main()*/

  5. missing include statement in above comments

    add the following :-

    #include
    #include

  6. i want a algorithm for doubly linked list

  7. i want logic to delete an element in double linked list

  8. both of above prog are not working properly.. Here is another small prog. of DLL. Works correctly on any compiler :)
    #include
    #include
    #include

    struct node
    {
    int data;
    struct node *pre;
    struct node *next;
    }*first;

    void insert();
    void del();
    void display();

    void main()
    {
    int ch;
    clrscr();
    first=NULL;
    do
    {
    printf(“\nEnter your choice\n”);
    printf(“1-Insert\n2-Remove\n3-Display\n4-Exit\n”);
    scanf(“%d”,&ch);
    switch(ch)
    {
    case 1: insert();
    break;
    case 2:del();
    break;
    case 3: display();
    break;
    }
    }while(chnext=NULL;
    t->pre=NULL;
    printf(“\nenter element to insert”);
    scanf(“%d”,&t->data);
    if(first==NULL)
    {
    first=t;
    }
    else
    {
    printf(“where u want to insert new node:\n”);
    printf(“1-at beginning\n2-at the end\n3-at selected position\n”);
    scanf(“%d”,&ch);
    switch(ch)
    {
    case 1: r=first;
    first=t;
    t->next=r;
    t->pre=NULL;
    //t->next=first;
    //first->pre=t;
    //t->pre=NULL;
    //first=t;
    break;
    case 2: r=first;
    while(r->next!=NULL)
    {
    r=r->next;
    }
    r->next=t;
    t->pre=r;//try after commenting this stmt
    t->next=NULL;
    break;
    case 3: printf(“\n enter element after which do you insert this element ?”);
    scanf(“%d”,&pos);
    r=first;
    f=0;
    do
    {
    if(r->data==pos);
    {
    f=1;
    s=r->next;
    r->next=t;
    t->next=s;
    t->pre=r;
    s->pre=t;
    }
    r=r->next;
    }while(r->data==pos);
    if(f==0)
    {
    printf(“Position not found, new node is dropped\n”);
    }
    }
    }
    }

    void del()
    {
    int pos,ch;
    struct node *r,*s,*t;
    if(first==NULL)
    {
    printf(“Linked list is Empty\n”);
    }
    else
    {
    printf(“Which element you want to remove\n”);
    printf(“1-first\n2-last\n3-selected\n”);
    scanf(“%d”,&ch);
    switch(ch)
    {
    case 1:first=first->next;
    first->pre=NULL;
    break;
    case 2:r=first;
    while(r->next!=NULL)
    {
    r=r->next;
    }
    r->pre=s;
    s->next=NULL;
    break;
    case 3:printf(“\nEnter which element you want to remove\n”);
    scanf(“%d”,&pos);
    r=first;
    while(r->data!=pos)
    {
    r=r->next;
    }
    s=r->pre;
    t=r->next;
    s->next=t;
    t->pre=s;
    break;
    }
    }
    }

    void display()
    {
    struct node *r;
    if(first==NULL)
    {
    printf(“\nLinked list is Empty\n”);
    }
    else
    {
    printf(“List contains:\n”);
    r=first;
    while(r!=NULL)
    {
    printf(“%d\t”,r->data);
    r=r->next;
    }
    //printf(“%d”,r->data);
    }

    }

  9. hi, i need a help for making double link list program in C double link list (file.h and file.c) in prototype insert first, insert after, insert last, delete first, delete after, and delete last. this program required in hospitaL

    you can email me : mesanda@gmail.com (zip)
    i need for your help
    thank you

  10. That piece of code is really well written and 100% correct. It did work on my lappy. Thanks bro… trying to understand the logics of linked list and this was of great help. Nicely explained codes.

  11. a) Develop a Single or a Double Linked List in C/C++
    b) Develop a Binary Tree in Java. And Show how to search in Java

    Hello Friends Can Any Body Plz Send Me This Program With Clear Explanation
    Thanking You Sir,

  12. it’s to hard….
    huhuhu:-(

  13. Thanks yaar………….

  14. typedef struct node
    {
    int data;
    struct node *next;
    struct node *prev;
    }NODE;

    typedef struct list
    {
    struct list *head;
    struct list *tail;
    }LIST;

    i want programm of dll using above two structures.please provideme that as early as possible

  15. Hey the first node of a linked list does not contain any data, it just has a link pointing to the next node which contains data.
    Here in this code the header(i.e. start) is made to have data!

    tmp->info=num;
    tmp->next=NULL;
    if(start==NULL)
    {
    tmp->prev=NULL;
    start->prev=tmp;
    start=tmp;
    }

  16. #include
    #include

    typedef struct Node
    {
    int data;
    struct Node *next;
    struct Node *prev;
    }node;

    void insert(node *pointer, int data)
    {
    /* Iterate through the list till we encounter the last node.*/
    while(pointer->next!=NULL)
    {
    pointer = pointer -> next;
    }
    /* Allocate memory for the new node and put data in it.*/
    pointer->next = (node *)malloc(sizeof(node));
    (pointer->next)->prev = pointer;
    pointer = pointer->next;
    pointer->data = data;
    pointer->next = NULL;
    }

    int find(node *pointer, int key)
    {
    pointer = pointer -> next; //First node is dummy node.
    /* Iterate through the entire linked list and search for the key. */
    while(pointer!=NULL)
    {
    if(pointer->data == key) //key is found.
    {
    return 1;
    }
    pointer = pointer -> next;//Search in the next node.
    }
    /*Key is not found */
    return 0;
    }

    void delete(node *pointer, int data)
    {
    /* Go to the node for which the node next to it has to be deleted */
    while(pointer->next!=NULL && (pointer->next)->data != data)
    {
    pointer = pointer -> next;
    }
    if(pointer->next==NULL)
    {
    printf(“Element %d is not present in the list
    “,data);
    return;
    }
    /* Now pointer points to a node and the node next to it has to be removed */
    node *temp;
    temp = pointer -> next;
    /*temp points to the node which has to be removed*/
    pointer->next = temp->next;
    temp->prev = pointer;
    /*We removed the node which is next to the pointer (which is also temp) */
    free(temp);
    /* Beacuse we deleted the node, we no longer require the memory used for it .
    free() will deallocate the memory.
    */
    return;
    }
    void print(node *pointer)
    {
    if(pointer==NULL)
    {
    return;
    }
    printf(“%d “,pointer->data);
    print(pointer->next);
    }

    int main()
    {
    /* start always points to the first node of the linked list.
    temp is used to point to the last node of the linked list.*/
    node *start,*temp;
    start = (node *)malloc(sizeof(node));
    temp = start;
    temp -> next = NULL;
    temp -> prev = NULL;
    /* Here in this code, we take the first node as a dummy node.
    The first node does not contain data, but it used because to avoid handling special cases
    in insert and delete functions.
    */
    printf(“1. Insert
    “);
    printf(“2. Delete
    “);
    printf(“3. Print
    “);
    printf(“4. Find
    “);
    while(1)
    {
    int query;
    scanf(“%d”,&query);
    if(query==1)
    {
    int data;
    scanf(“%d”,&data);
    insert(start,data);
    }
    else if(query==2)
    {
    int data;
    scanf(“%d”,&data);
    delete(start,data);
    }
    else if(query==3)
    {
    printf(“The list is “);
    print(start->next);
    printf(”
    “);
    }
    else if(query==4)
    {
    int data;
    scanf(“%d”,&data);
    int status = find(start,data);
    if(status)
    {
    printf(“Element Found
    “);
    }
    else
    {
    printf(“Element Not Found
    “);

    }
    }
    }

    }

  17. Can some one please explain me the code, if possible line by line.

    Thank you, Ajay

  18. i want stutent data using linked list c program

  19. Hi, i am Mohan if possible to add no of data in single node using doubly linked list, (i.e) my structure having no of member like

    struct node
    {
    struct node *prev;
    int one,int two,int three,int four,int five,int six,int seven;
    struct node *next;
    }*start

    consider the above struct, in the 7 member of data to be stored in single node, pleace reply

  20. Good afternoon sir ….
    there is a problem when the node is the first to be created .
    In this function

    create_list(int num)
    {
    struct node *q,*tmp;
    tmp= malloc(sizeof(struct node));
    tmp->info=num;
    tmp->next=NULL;
    if(start==NULL)
    {
    tmp->prev=NULL;
    start->prev=tmp; // if start is null how to get his field .> prev ? must to create start before , or not
    start=tmp;
    }

    thanks and regards

  21. i had arrange in this way , pratically i removed with comments this line
    // start->prev=tmp;
    Now it is workinking and i’m happy :)
    thank you sir

  22. format penyimpanan nya apa bray dan bagai mana menjalan kan nya?

  23. Thanks a lots…..

  24. thanks a lots…………………>

Leave a Reply