Pages

Custom Search

Sunday, October 16, 2011

Program to insert and delete node at the end of Doubly Linked List




 #include <stdio.h>  
 #include <conio.h>  
 struct tag  
 {  
     struct tag* llink;  
     int data;  
     struct tag* rlink;  
 };  
 typedef struct tag * NODE;  
 void ins_rear();  
 void del_rear();  
 void display();  
 int main()  
 {  
     NODE first=NULL;  
     int choice, ele;  
     do  
     {  
         printf("\nDoubly linked list operations: ");  
         printf("\n1.) Insert front");  
         printf("\n2.) Delete front");  
         printf("\n3.) Display");  
         printf("\n4.) Exit");  
         printf("\n\nEnter your choice: ");  
         scanf("%d", &choice);  
         switch(choice)  
         {  
             case 1: printf("\nEnter element to insert at rear: ");  
                 scanf("%d", &ele);  
                 ins_rear(&first, ele);  
                 break;  
             case 2: del_rear(&first);  
                 break;  
             case 3: display(&first);  
                 break;  
             case 4: break;  
             default: printf("\nWrong choice!! Enter choice again.");  
         }  
     }while(choice != 4);  
     getch();  
 }  
 //Function to insert node at rear   
 void ins_rear(NODE *first, int ele)  
 {  
     NODE newnode, temp;  
     newnode = (NODE)malloc(sizeof(struct tag));  
     if(newnode == NULL)  
     {  
         printf("\nInsufficient memory");  
         return;  
     }              //checking if memory is allocated successfully  
     newnode->data = ele;  
     newnode->rlink = NULL;  
     if(*first == NULL)        //If list is empty  
     {  
         newnode->llink = NULL;  
         *first = newnode;  
     }  
     else        //If list contains one or more nodes  
     {  
         temp = *first;  
         while(temp->rlink != NULL)     //Traversing Doubly L.L  
         {  
             temp = temp->rlink;  
         }  
         temp->rlink = newnode;  
         newnode->llink = temp;  
     }  
 }  
 //Function to delete rear node  
 void del_rear(NODE *first)  
 {  
     NODE temp;  
     if(*first == NULL)  
     {  
         printf("\nDoubly linked list is empty");  
     }  
     else if((*first)->rlink == NULL)     //If only one node is present  
     {  
         temp= *first;  
         printf("\nDeleted node: %d", temp->data);  
         *first = NULL;  
         free(temp);  
     }  
     else        //If more than one nodes are present  
     {  
         temp = *first;  
         while(temp->rlink != NULL)  
         {  
             temp = temp->rlink;  
         }  
         printf("\nDeleted node: %d", temp->data);  
         temp->llink->rlink = NULL;  
         free(temp);  
     }  
 }  
 //Function to display contents of doubly linked list  
 void display(NODE *first)  
 {  
     NODE temp;  
     if(*first == NULL)  
     {  
         printf("\nDoubly linked list is empty");  
     }  
     else  
     {  
         temp = *first;  
         while(temp!=NULL)  
         {  
             printf("%d ", temp->data);  
             temp=temp->rlink;  
         }  
     }  
 }  

No comments:

Custom Search