Thursday, 16 November 2017

Double link list using C

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
    struct node *prev;
};

struct node *create(struct node *head)
{
    struct node *newnode,*ptr;
    int val;
    printf("Enter -1 to stop\n");
    printf("Enter the value of node:\n");
    scanf("%d", &val);
    while(val!=-1)
    {
        newnode=(struct node*)malloc(sizeof(struct node));
        newnode -> data= val;
        if(head==NULL)
        {
            head=newnode;
            newnode->prev=NULL;
            newnode->next=NULL;
        }
        else
        {
            ptr=head;
            while(ptr->next != NULL)
            {
                ptr=ptr->next;
            }
            ptr->next =newnode;
        }
        newnode->prev=NULL;
        newnode->next=NULL;
        printf("Enter the value of node\n");
        scanf("%d",&val);
   }
   return head;
}
struct node *display(struct node *head)
{
    if(head==NULL)
    {
        printf("Linked list is not created\n");
    }
    else
    {
        struct node *ptr;
        ptr=head;
        while(ptr!= NULL)
        {
            printf("%d ",ptr->data);
            ptr=ptr->next;
        }
    }
    return head;
}

struct node *insBeg(struct node *head)
{
    struct node *newnode;
    newnode=(struct node*)malloc(sizeof(struct node));
    if(head==NULL)
    {
        printf("Linked list is not created\n");
    }
    else
    {
        int val;
        printf("Enter the value of node\n");
        scanf("%d",&val);
        newnode->data=val;
        newnode->prev=NULL;
        newnode->next=head;
        head=newnode;
    }
    return head;
}
struct node *insEnd(struct node *head)
{
    if(head==NULL)
    {
        printf("Linked list is not created\n");
    }
    else
    {
        struct node *newnode,*ptr;
        newnode=(struct node *)malloc(sizeof(struct node));
        int val;
        printf("4. Enter the value of node\n");
        scanf("%d",&val);
        newnode->data = val;
        ptr=head;
        while(ptr->next !=NULL)
        {
            ptr=ptr->next;
        }
        ptr->next =newnode;
        newnode->prev=ptr;
        newnode->next =NULL;
    }
    return head;
}
struct node *insBef(struct node *head)
{
    if(head==NULL)
    {
        printf("Linked list is not created\n");
    }
    else
    {
        struct node *ptr,*pptr;
        ptr=head;
        pptr=ptr->next;
        int n,val;
        printf("Enter the value before which you want to insert\n");
        scanf("%d",&n);
        if(ptr->data==n)
        {
            head=insBeg(head);
        }
        else
        {
            while(pptr->data!=n)
            {
                ptr=pptr;
                pptr=pptr->next;
                if(pptr==NULL)
                {
                    printf("Value not in the list\n");
                    return head;
                }
            }
            struct node *newnode;
            newnode=(struct node*)malloc(sizeof(struct node));
            printf("Enter the value of newnode\n");
            scanf("%d",&val);
            newnode->next=pptr;
            ptr->next=newnode;
            newnode->prev=ptr;
            newnode->data=val;
        }

    }
    return head;
}

struct node *insAft(struct node *head)
{
    if(head==NULL)
    {
        printf("Linked list is not created\n");
    }
    else
    {
            struct node *ptr,*pptr;
            ptr=head;
            pptr=ptr->next;
            int n;
            printf("Enter the value of node after which you want to insert\n");
            scanf("%d",&n);
            while(ptr->data!=n)
            {
                ptr=pptr;
                if(ptr==NULL)
                {
                    printf("Node doesn't exist in the list\n");
                    return head;
                }
                pptr=pptr->next;
            }
            struct node *newnode;
            newnode=(struct node*)malloc(sizeof(struct node));
            int val;
            printf("Enter the value of node\n");
            scanf("%d",&val);
            newnode->next=pptr;
            ptr->next=newnode;
            newnode->prev=ptr;
            newnode->data=val;

    }
    return head;
}
struct node *posIns(struct node *head)
{
    int n=0,k=2;

    printf("\nEnter the position at which you want to insert\n");
    scanf("%d",&n);

    if(head==NULL)
    {
        printf("Linked list is not created\n");
    }
    else
    {
        int c;
        struct node *ptr;
        ptr=head;
        while(ptr!=NULL)
        {
            c++;
            ptr=ptr->next;
        }
        if(n<=0 || n>(c+1))
        {
            printf("Insert a valid position\n");
        }
        else if(n==1)
        {
            head=insBeg(head);
        }
        else
        {
            int val;
            struct node *newnode;
            newnode=(struct node*)malloc(sizeof(struct node));
            printf("Enter the value of node\n");
            scanf("%d",&val);
            newnode->data=val;
            ptr=head;
            if(k==n)
            {
                newnode->next=ptr->next;
                ptr->next=newnode;
                newnode->prev=ptr;
            }
            else
            {
                while(k!=n)
                {
                    ptr=ptr->next;
                    k++;
                }
                if(ptr==NULL)
                    printf("\nPlease enter a valid option\n");
                else
                {
                    newnode->next=ptr->next;
                    ptr->next=newnode;
                    newnode->prev=ptr;
                }

            }
        }
    }
    return head;
}
struct node *delBeg(struct node *head)
{
    if(head==NULL)
    {
        printf("Linked list is not created\n");
    }
    else
    {
        struct node *ptr;
        ptr=head;
        head=head->next;
        head->prev=NULL;
        free(ptr);
    }
    return head;
}
struct node *delEnd(struct node *head)
{
    if(head==NULL)
    {
        printf("Linked list is not created\n");
    }
    else
    {
        struct node *ptr,*preptr;
        if(head->next==NULL)
        {
            head=NULL;
            free(head);
        }
        else
        {
            ptr=head;
            while(ptr->next!=NULL)
            {
                preptr=ptr;
                ptr=ptr->next;
            }
            preptr->next=NULL;
            free(ptr);
        }
    }
    return head;
}
struct node *delBef(struct node *head)
{
    if(head==NULL)
    {
        printf("Linked list is not created\n");
    }
    else
    {
        struct node *ptr,*pptr;
        ptr=head;
        pptr=ptr->next;
        int n;
        printf("Enter the value of node before which you want to delete\n");
        scanf("%d",&n);
        if(ptr->data==n)
        {
            printf("Deletion is not possible\n");
        }
        else if(pptr->data==n)
        {
            head=delBeg(head);
        }
        else
        {
            while(pptr->next->data!=n)
            {
                ptr=pptr;
                pptr=pptr->next;
                if(pptr->next==NULL)
                {
                    printf("Invalid choice\n");
                    return head;
                }
            }
            ptr->next=pptr->next;
            free(pptr);
        }

    }
    return head;
}
struct node *delAft(struct node *head)
{
    if(head==NULL)
    {
        printf("Linked list is not created\n");
    }
    else
    {
        struct node *ptr,*pptr;
        ptr=head;
        pptr=ptr->next;
        int n;
        printf("Enter the value of node after which you want to delete\n");
        scanf("%d",&n);
        while(ptr->data!=n)
        {
            ptr=pptr;
            if(ptr->next==NULL)
            {
                printf("\nPlease enter a valid value\n");
                return head;
            }
            pptr=pptr->next;
        }
        ptr->next=pptr->next;
        free(pptr);
    }
    return head;
}

struct node *posDel(struct node *head)
{
     int n=0,k=2;

    printf("\nEnter the position at which you want to delete\n");
    scanf("%d",&n);

    if(head==NULL)
    {
        printf("Linked list is not created\n");
    }
    else
    {
        int c=0;;
        struct node *ptr,*pptr;
        ptr=head;
        while(ptr!=NULL)
        {
            c++;
            ptr=ptr->next;
        }
        if(n<=0 || n>c)
        {
            printf("insert a valid position\n");
        }
        else if(n==1)
        {
            head=delBeg(head);
        }
        else
        {
            ptr=head;
            pptr=ptr->next;
            while(k!=n)
            {
                ptr=ptr->next;
                pptr=ptr->next;
                k++;
            }
            if(ptr==NULL)
                printf("\nPlease enter a valid option\n");
            else
            {
                ptr->next=pptr->next;
                free(pptr);
            }
        }
    }
    return head;
}

int main()
{
    int num;
    struct node *head=NULL;
    do
    {
        printf("Enter your option\n");
        printf("1. Creation or insertion of doubly linked list\n");
        printf("2. Deletion\n");
        printf("Enter -1 to exit\n");
        scanf("%d",&num);
        switch(num)
        {
            case 1:
                {
                     do
                     {

                           printf("\n1. Create a doubly linked list \n");
                           printf("2. Display the linked list \n");
                           printf("3. Insert a node at the beginning \n");
                           printf("4. Insert a node at the end \n");
                           printf("5. Insert a node before a given node \n");
                           printf("6. Insert a node after a given node \n");
                           printf("7. Insert a node at a position\n");
                           printf("Enter your option \n");
                           printf("Enter -1 to exit\n");
                           scanf("%d",&num);
                           switch(num)
                           {
                               case 1:
                                   head=create(head);
                                   break;
                               case 2:
                                    head=display(head);
                                    break;
                               case 3:
                                    head=insBeg(head);
                                    break;
                               case 4:
                                    head=insEnd(head);
                                    break;
                               case 5:
                                    head=insBef(head);
                                    break;
                               case 6:
                                    head=insAft(head);
                                    break;
                               case 7:
                                    head=posIns(head);
                           }
                       }while(num!=-1);
                       break;
                }
                case 2:
                        {
                            do
                            {
                                printf("\n\n1. Display\n");
                                printf("2. Delete a node at the beginning\n");
                                printf("3. Delete a node at the end\n");
                                printf("4. Delete a node before a given node\n");
                                printf("5. Delete a node after a given node\n");
                                printf("6. Delete a node at a position\n");
                                printf("Enter -1 to exit\n");
                                printf("Enter your option\n");
                                scanf("%d",&num);
                                switch(num)
                                {
                                case 1:
                                    {
                                        head=display(head);
                                        break;
                                    }
                                case 2:
                                    {
                                        head=delBeg(head);
                                        break;
                                    }
                                case 3:
                                    {
                                        head=delEnd(head);
                                        break;
                                    }
                                case 4:
                                    {
                                        head=delBef(head);
                                        break;
                                    }
                                case 5:
                                    {
                                        head=delAft(head);
                                        break;
                                    }
                                case 6:
                                    {
                                        head=posDel(head);
                                        break;
                                    }
                                }
                            }while(num!=-1);
                            break;
                        }
        }
        printf("Enter your option\n");
        printf("Enter any no. to continue Insertion or Deletion\n");
        printf("Enter -1 to exit\n");
        scanf("%d",&num);
    }while(num!=-1);

   return 0;
}


All In One Blog

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.

0 comments:

Post a Comment

 

Copyright @ 2015