Doubly linked list code output

Code

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

typedef struct node
{
        int id;
        struct node *pre,*next;            //for storing address of next node
}node;

node* create();
node* append(node*);
node* del(node*);
node* modify(node*);
void display(node*);

node* create()
{
    node* head;
    head=(node*)malloc(sizeof(node));  /*allocating memory to node
                                         and head is pointing to it  */

    printf("\nEnter employee id ");
    scanf("%d",&head->id);

    head->pre=head->next=NULL;
    return head;
}

node* append(node* head)
{
    node* curr = (node*)malloc(sizeof(node));  //allocate memory for current node

    node* prev=head;

    printf("\nEnter employee id ");
    scanf("%d",&curr->id);

    while(prev->next!=NULL)                    //traverse the list and go to the last node
         prev=prev->next;

    prev->next=curr;                          /*establish the link of last node(pointed by
                                                prev) and current node   */

    curr->pre = prev;
    curr->next=NULL;

    return head;
}

node* modify(node* head)
{
    node* curr=head;
    int eid,found=0;

    printf("\nEnter Eid of employee whose data is to be modified ");
    scanf("%d",&eid);

    while(curr!=NULL)                                     //traverse list in search of eid
    {
           if(curr->id==eid)
           {
              found = 1;
              break;
           }
           else
              curr=curr->next;
    }

    if(found == 1)                                       //eid is found
    {
            printf("\nEnter employee id ");
            scanf("%d",&curr->id);
    }
    else
            printf("\nEmployee id not found");

    return head;
}

node* del(node* head)
{
    node *prev,*curr=head;
    int eid;

    printf("\nPlease enter employee id to be deleted ");
    scanf("%d",&eid);

    if(head==NULL)
         return NULL;

    else if(curr->id==eid && curr->next==NULL)          //only 1 node
         head=NULL;

    else if(curr->id==eid && curr->next!=NULL)          //1st node
    {
         head=head->next;
         head->pre=NULL;
    }
    else
    {
         while(curr!=NULL)                              //search eid
         {
              if(curr->id==eid)
                  break;
              else
              {
                  prev=curr;
                 curr=curr->next;
              }
         }

         if(curr!=NULL)                             //if eid found
         {      
              prev->next=curr->next;               /*establish the link between node pointed by prev
                                  & node next to node pointed by curr */
              (*(curr->next)).pre = prev;
         }
    }

    if(curr!=NULL)                                 //eid found
         free(curr);
    else
         printf("\nEmployee id not found");

    return head;
}

void display(node* head)
{
    node* curr=head;

    if(curr==NULL)
        printf("\nList is empty");
    else
    {
        printf("\n");

        while(curr!=NULL)
        {
            printf("%d---",curr->id);
            curr=curr->next;
        }
    }
}

int main()
{
    node* head = NULL;
    int ch;

    do
    {
        printf("\n1.Create   2.Append   3.Modify   4.Delete   5.Display\nEnter choice ");
        scanf("%d",&ch);

        switch(ch)
        {
            case 1:
                head = create();
                break;
            case 2:
                head = append(head);
                break;

            case 3:
                head = modify(head);
                break;


            case 4:
                head = del(head);
                break;

            case 5:
                display(head);
                break;

        }

        printf("\nContinue? ");
        fflush(stdin);

    }while(getchar()=='y');

    return 0;    
}


O/P

Doubly-linked-list-output


No comments:

Post a Comment