Sunday, September 10, 2017

Program to Implement Doubly Linked List in C

/*==========================================================================================
 **
 **  File Name     : dll.c
 **  Creation Date : Sun 10 Sep 2017 09:22:08 PM IST
 **  Last Modified : Sun 10 Sep 2017 10:38:41 PM IST
 **  Compiler      : gcc
 **  Author        : Manoj Kumar Patra, Asst. Professor
 **  Organization  : Vignan University, Guntur, India.
 **
 **=========================================================================================*/

/*Program to Implement Doubly Linked List*/

#include<stdio.h>
#include<stdlib.h>

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

struct node *create_dll(struct node *start);
void display(struct node *start);
struct node *add_at_beg(struct node *start, int data);
struct node *add_at_end(struct node *start, int data);
struct node *add_after_node(struct node *start, int data, int item);
struct node *add_before_node(struct node *start, int data, int item);
struct node *del_at_beg(struct node *start);
struct node *del_at_end(struct node *start);
struct node *del_a_node(struct node *start, int data);
struct node *reverse(struct node *start);

main()
{
    int choice,data,item;
    struct node *start=NULL;
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
    printf("= = = = = = = = = = Doubly Linked List in C = = = = = = = = = =\n");
    while(1)
    {
        printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
        printf("1.Create DLL\n");
        printf("2.Display DLL\n");
        printf("3.Add at beg\n");
        printf("4.Add at end\n");
        printf("5.Add after node\n");
        printf("6.Add before node\n");
        printf("7.Delete from beg\n");
        printf("8.Delete from end\n");
        printf("9.Delete a node\n");
        printf("10.Reverse\n");
        printf("11.Exit\n");
        printf("\nEnter your choice : ");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
                start=create_dll(start);
                display(start);
                break;
            case 2:
                display(start);
                break;
            case 3:
                printf("Enter the element to be inserted: ");
                scanf("%d",&data);
                start=add_at_beg(start,data);
                printf("The resultant DLL\n");
                display(start);
                break;
            case 4:
                printf("Enter the element to be inserted: ");
                scanf("%d",&data);
                start=add_at_end(start,data);
                printf("The resultant DLL\n");
                display(start);
                break;
            case 5:
                printf("Enter the element to be inserted: ");
                scanf("%d",&data);
                printf("Enter the element after which to insert: ");
                scanf("%d",&item);
                start=add_after_node(start,data,item);
                printf("The resultant DLL\n");
                display(start);
                break;
            case 6:
                printf("Enter the element to be inserted: ");
                scanf("%d",&data);
                printf("Enter the element before which to insert: ");
                scanf("%d",&item);
                start=add_before_node(start,data,item);
                printf("The resultant DLL\n");
                display(start);
                break;
            case 7:
                start=del_at_beg(start);
                printf("The resultant DLL\n");
                display(start);
                break;
            case 8:
                start=del_at_end(start);
                printf("The resultant DLL\n");
                display(start);
                break;
            case 9:
                printf("Enter the element to be deleted: ");
                scanf("%d",&data);
                start=del_a_node(start,data);
                printf("The resultant DLL\n");
                display(start);
                break;
            case 10:
                start=reverse(start);
                printf("The resultant DLL\n");
                display(start);
                break;
            case 11:
                printf("Program Terminated..!!\n");
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                exit(1);
            default:
                printf("Oops.. Wrong choice !\n");
        }
    }
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
}

//1
struct node *create_dll(struct node *start)
{
    int i,n,data;
    printf("Enter the number of nodes: ");
    scanf("%d",&n);
    start=NULL;
    if(n==0)
        return start;
    printf("Enter the element to be inserted : ");
    scanf("%d",&data);
    struct node *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->info=data;
    tmp->prev=NULL;
    tmp->next=NULL;
    start=tmp;
    if(n>1)
    {
        for(i=2; i<=n; i++)
        {
            printf("Enter the element to be inserted : ");
            scanf("%d",&data);
            start=add_at_end(start,data);
        }

    }
    return start;
}

//2
void display(struct node *start)
{
    struct node *p;
    if(start==NULL)
    {
        printf("List is empty\n");
        return;
    }
    p=start;
    printf("The elements of DLL are:\n");
    while(p!=NULL)
    {
        printf("%d\t",p->info);
        p=p->next;
    }
    printf("\n");
}

//3
struct node *add_at_beg(struct node *start,int data)
{
    struct node *tmp;
    tmp = (struct node *)malloc(sizeof(struct node));
    tmp->info=data;
    tmp->prev=NULL;
    tmp->next=start;
    start->prev=tmp;
    start=tmp;
    return start;
}

//4
struct node *add_at_end(struct node *start,int data)
{
    struct node *tmp,*p;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->info=data;
    p=start;
    while(p->next!=NULL)
        p=p->next;
    p->next=tmp;
    tmp->next=NULL;
    tmp->prev=p;
    return start;
}

//5
struct node *add_after_node(struct node *start,int data,int item)
{
    struct node *tmp,*p;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->info=data;
    p=start;
    while(p!=NULL)
    {
        if(p->info==item)
        {
            tmp->prev=p;
            tmp->next=p->next;
            if(p->next!=NULL)
                p->next->prev=tmp;
            p->next=tmp;
            return start;  
        }
        p=p->next;
    }
    printf("%d not present in the list\n\n",item);
    return start;
}

//6
struct node *add_before_node(struct node *start,int data,int item)
{
    struct node *tmp,*q;
    if(start==NULL )
    {
        printf("List is empty\n");
        return start;
    }
    if(start->info==item)
    {
        tmp = (struct node *)malloc(sizeof(struct node));
        tmp->info=data;
        tmp->prev=NULL;
        tmp->next=start;
        start->prev=tmp;
        start=tmp;
        return start;
    }
    q=start;
    while(q!=NULL)
    {
        if(q->info==item)
        {
            tmp=(struct node *)malloc(sizeof(struct node));
            tmp->info=data;
            tmp->prev=q->prev;
            tmp->next = q;
            q->prev->next=tmp;
            q->prev=tmp;
            return start;
        }  
        q=q->next;
    }
    printf("%d not present in the list\n",item);
    return start;
}      

//7
struct node *del_at_beg(struct node *start)
{
    if(start==NULL)
    {
        printf("List is empty\n");
        return start;
    }
    struct node *tmp;
    tmp=start;
    start=start->next;
    start->prev=NULL;
    free(tmp);
    printf("First node has been deleted..!\n");
    return start;
}

//8
struct node *del_at_end(struct node *start)
{
    if(start==NULL)
    {
        printf("List is empty\n");
        return start;
    }
    struct node *ptmp, *tmp;
    tmp=start;
    while(tmp->next!=NULL)
    {
        ptmp=tmp;
        tmp=tmp->next;
    }
    ptmp->next=NULL;
    free(tmp);
    return start;
}

//9
struct node *del_a_node(struct node *start,int data)
{
    if(start==NULL)
    {
        printf("List is empty\n");
        return start;
    }
    struct node *tmp;
    tmp=start->next;
    while(tmp->next!=NULL )
    {
        if(tmp->info==data)   
        {
            tmp->prev->next=tmp->next;
            tmp->next->prev=tmp->prev;
            free(tmp);
            return start;
        }
        tmp=tmp->next;
    }
    printf("Element %d not found\n",data);
    return start;
}

//10
struct node *reverse(struct node *start)
{
    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;
    }
    start=p1;
    printf("List reversed\n");
    return start;
}

OutPut:

manoj@ubuntu:~/cp/linkedlist$ gcc dll.c -o dll
manoj@ubuntu:~/cp/linkedlist$ ./dll
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
= = = = = = = = = = Doubly Linked List in C = = = = = = = = = =
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1.Create DLL
2.Display DLL
3.Add at beg
4.Add at end
5.Add after node
6.Add before node
7.Delete from beg
8.Delete from end
9.Delete a node
10.Reverse
11.Exit

Enter your choice : 1
Enter the number of nodes: 4
Enter the element to be inserted : 4
Enter the element to be inserted : 5
Enter the element to be inserted : 6
Enter the element to be inserted : 7
The elements of DLL are:
4    5    6    7   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1.Create DLL
2.Display DLL
3.Add at beg
4.Add at end
5.Add after node
6.Add before node
7.Delete from beg
8.Delete from end
9.Delete a node
10.Reverse
11.Exit

Enter your choice : 2
The elements of DLL are:
4    5    6    7   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1.Create DLL
2.Display DLL
3.Add at beg
4.Add at end
5.Add after node
6.Add before node
7.Delete from beg
8.Delete from end
9.Delete a node
10.Reverse
11.Exit

Enter your choice : 3
Enter the element to be inserted: 3
The resultant DLL
The elements of DLL are:
3    4    5    6    7   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1.Create DLL
2.Display DLL
3.Add at beg
4.Add at end
5.Add after node
6.Add before node
7.Delete from beg
8.Delete from end
9.Delete a node
10.Reverse
11.Exit

Enter your choice : 4
Enter the element to be inserted: 8
The resultant DLL
The elements of DLL are:
3    4    5    6    7    8   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1.Create DLL
2.Display DLL
3.Add at beg
4.Add at end
5.Add after node
6.Add before node
7.Delete from beg
8.Delete from end
9.Delete a node
10.Reverse
11.Exit

Enter your choice : 5
Enter the element to be inserted: 9
Enter the element after which to insert: 5
The resultant DLL
The elements of DLL are:
3    4    5    9    6    7    8   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1.Create DLL
2.Display DLL
3.Add at beg
4.Add at end
5.Add after node
6.Add before node
7.Delete from beg
8.Delete from end
9.Delete a node
10.Reverse
11.Exit

Enter your choice : 6
Enter the element to be inserted: 10
Enter the element before which to insert: 5
The resultant DLL
The elements of DLL are:
3    4    10    5    9    6    7    8   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1.Create DLL
2.Display DLL
3.Add at beg
4.Add at end
5.Add after node
6.Add before node
7.Delete from beg
8.Delete from end
9.Delete a node
10.Reverse
11.Exit

Enter your choice : 7
First node has been deleted..!
The resultant DLL
The elements of DLL are:
4    10    5    9    6    7    8   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1.Create DLL
2.Display DLL
3.Add at beg
4.Add at end
5.Add after node
6.Add before node
7.Delete from beg
8.Delete from end
9.Delete a node
10.Reverse
11.Exit

Enter your choice : 8
The resultant DLL
The elements of DLL are:
4    10    5    9    6    7   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1.Create DLL
2.Display DLL
3.Add at beg
4.Add at end
5.Add after node
6.Add before node
7.Delete from beg
8.Delete from end
9.Delete a node
10.Reverse
11.Exit

Enter your choice : 9
Enter the element to be deleted: 10
The resultant DLL
The elements of DLL are:
4    5    9    6    7   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1.Create DLL
2.Display DLL
3.Add at beg
4.Add at end
5.Add after node
6.Add before node
7.Delete from beg
8.Delete from end
9.Delete a node
10.Reverse
11.Exit

Enter your choice : 11
Program Terminated..!!
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/cp/linkedlist$

No comments:

Post a Comment