google.com, pub-8786015629279405, DIRECT, f08c47fec0942fa0 Write a Program to implementation of Doubly Linked List in C

Write a Program to implementation of Doubly Linked List in C

0

 


 

Write a Program to implementation of Doubly Linked List in C

 

 

#include<stdio.h> 

#include<stdlib.h> 

struct node 

{ 

    struct node *prev; 

    struct node *next; 

    int data; 

}; 

struct node *start; 

/*fuction declaration of all the operations*/

void insert_begin();  

void insert_last(); 

void insert_locc(); 

void delete_begin(); 

void delete_last(); 

void delete_locc(); 

void print(); 

void main () 

{ 

int ch=0; 

    while(ch!=8) 

    { 

        printf("\nEnter the operation to be performed\n"); 

        printf("\n1.Insert in the begining\n2.Insert at last\n3.Insert at any specified position\n4.Delete from Beginning\n5.Delete from last\n6.Delete node after specified location\n7.Print\n8.Exit\n"); 

        scanf("\n%d",&ch); 

        switch(ch) 

        { 

                 /*function calls of all the operations */

            case 1: 

            insert_begin();      

            break; 

            case 2: 

            insert_last();        

            break; 

            case 3: 

            insert_locc();      

            break; 

            case 4: 

            delete_begin();      

            break; 

            case 5: 

            delete_last();       

            break; 

            case 6: 

            delete_locc();          

            break; 

            case 7: 

            print();       

            break; 

            case 8: 

            exit(0); 

            break; 

            default: 

            printf("Enter valid option");

        } 

    } 

}  /*function deefinition*/

void insert_begin()      //to insert the node in the beginning

{ 

   struct node *p;  

   int value; 

   p=(struct node *)malloc(sizeof(struct node)); 

   if(p==NULL) 

   { 

       printf("\nOVERFLOW"); 

   } 

   else 

   { 

    printf("\nEnter value: "); 

    scanf("%d",&value); 

     

   if(start==NULL) 

   { 

       p->next=NULL; 

       p->prev=NULL; 

       p->data=value; 

       start=p; 

   } 

   else  

   { 

       p->data=value; 

       p->prev=NULL; 

       p->next=start; 

       start->prev=p; 

       start=p; 

   } 

} 

} 

void insert_last()            //to insert the node at the last of the list 

{ 

   struct node *p,*temp; 

   int value; 

   p=(struct node *)malloc(sizeof(struct node)); 

   if(p==NULL) 

   { 

       printf("\nOVERFLOW"); 

   } 

   else 

   { 

       printf("\nEnter value: "); 

       scanf("%d",&value); 

        p->data=value; 

       if(start==NULL) 

       { 

           p->next=NULL; 

           p->prev=NULL; 

           start=p; 

       } 

       else 

       { 

          temp=start; 

          while(temp->next!=NULL) 

          { 

              temp=temp->next; 

          } 

          temp->next=p; 

          p->prev=temp; 

          p->next=NULL; 

          }

       } 

    } 

void insert_locc()      //to insert the node at the specified location of the list

{ 

   struct node *p,*temp; 

   int value,loc,i; 

   p=(struct node *)malloc(sizeof(struct node)); 

   if(p==NULL) 

   { 

       printf("\n OVERFLOW"); 

   } 

   else 

   { 

       temp=start; 

       printf("Enter the location"); 

       scanf("%d",&loc); 

       for(i=0;i<loc;i++) 

       { 

           temp=temp->next; 

           if(temp==NULL) 

           { 

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

               return; 

           } 

       } 

       printf("Enter value: "); 

       scanf("%d",&value); 

       p->data=value; 

       p->next=temp->next; 

       p->prev=temp; 

       temp->next=p; 

       temp->next->prev=p;  

   } 

} 

void delete_begin()      //to delete the node present in the beginning of the list

{ 

    struct node *p; 

    if(start==NULL) 

    { 

        printf("\n UNDERFLOW"); 

    } 

    else if(start->next==NULL) 

    { 

        start=NULL;  

        free(start);  

    } 

    else 

    { 

        p=start; 

        start=start->next; 

        start->prev=NULL; 

        free(p);  

    } 

} 

void delete_last()    //to delete the node present in the last of the list

{ 

    struct node *p; 

    if(start==NULL) 

    { 

        printf("\n UNDERFLOW"); 

    } 

    else if(start->next==NULL) 

    { 

        start=NULL;  

        free(start);  

    } 

    else  

    { 

        p=start;  

        if(p->next!=NULL) 

        { 

            p=p->next;  

        } 

        p->prev->next=NULL;  

        free(p); 

    } 

} 

void delete_locc()    //to delete the node present at the specified of the list

{ 

    struct node *p, *temp; 

    int val; 

    printf("\n Enter the data after which the node is to be deleted : "); 

    scanf("%d", &val); 

    p=start; 

    while(p->data!=val) 

    p=p->next; 

    if(p->next==NULL) 

    { 

        printf("\nCan't delete\n"); 

    } 

    else if(p->next->next==NULL) 

    { 

        p->next=NULL; 

    } 

    else 

    {  

        temp=p->next; 

        p->next=temp->next; 

        temp->next->prev=p; 

        free(temp);  

    }    

} 

void print()  //to print the values in the list

{ 

    struct node *p; 

    printf("\nvalues are:\n"); 

    p=start; 

    while(p!=NULL) 

    { 

        printf("%d\n",p->data); 

        p=p->next; 

    } 

}  

 

 

OUTPUT:

 

 

Enter the operation to be performed

 

1.Insert in the begining

2.Insert at last

3.Insert at any specified position

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Print

8.Exit

1

 

Enter value: 89

 

Enter the operation to be performed

 

1.Insert in the begining

2.Insert at last

3.Insert at any specified position

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Print

8.Exit

1

 

Enter value: 65

 

Enter the operation to be performed

 

1.Insert in the begining

2.Insert at last

3.Insert at any specified position

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Print

8.Exit

1

 

Enter value: 78

 

Enter the operation to be performed

 

1.Insert in the begining

2.Insert at last

3.Insert at any specified position

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Print

8.Exit

2

 

Enter value: 84

 

Enter the operation to be performed

 

1.Insert in the begining

2.Insert at last

3.Insert at any specified position

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Print

8.Exit

7

 

values are:

78

65

89

84

 

Enter the operation to be performed

 

1.Insert in the begining

2.Insert at last

3.Insert at any specified position

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Print

8.Exit

5

 

Enter the operation to be performed

 

1.Insert in the begining

2.Insert at last

3.Insert at any specified position

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Print

8.Exit

7

 

values are:

78

 

Enter the operation to be performed

 

1.Insert in the begining

2.Insert at last

3.Insert at any specified position

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Print

8.Exit

8

 



Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Thank you for your interest 😊

We will back shortly after reviewing...

Thank you for your interest 😊

We will back shortly after reviewing...

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top