Tuesday, 12 July 2011

Delete a Node..

node* del(node *current)
{
    int userdata;               /*Data for deleting a node*/
    node *newnode,*temp;
    printf ("\nEnter the data whose node you want to delete\n");
    scanf("%d",&userdata);
    newnode=current;
    if(current->data==userdata)
    {
        /***  Checking condition for deletion of first node  ***/
        current=current->next;
        current->prev=NULL;
        free(newnode);
        return(current);
    }
    else
    {
        while(newnode->next->next!=NULL)
        {
            /***  Checking condition for deletion of   ***/
            /*** all nodes except first and last node  ***/
            if(current->next->data==userdata)
            {
                newnode=current;
                temp=current->next;
                newnode->next=newnode->next->next;
                newnode->next->prev=current;
                free(temp);
                return(current);
            }
            newnode=newnode->next;
        }
        if(newnode->next->next==NULL && newnode->next->data==userdata)
        {
            /***  Checking condition for deletion of last node  ***/
            temp=newnode->next;
            free(temp);
            newnode->next=NULL;
            return(current);
        }
    }
    printf("\nMatch not found\n");
    return(current);
}

No comments:

Post a Comment