Tuesday, 12 July 2011

Delete a Node..

node* del(node *start)
{
    int userdata;                    /* Data for deleting a node*/
    node *delnode;          /* Node to be deleted */
    node *current;              /* Node for travelling the linked list*/
    printf("\nEnter the roll number whose node you want to delete\n");
    scanf("%d",&userdata);
    current=start;
    if(current->data==userdata)
    {
        /***  Checking condition for deletion of first node  ***/
        delnode=current; /*  Unnecessary step  */
        while(current->next!=start)
            current=current->next;
        current->next=start->next;
        start=start->next;
        free(delnode);
        return(start);
    }
    else
    {
        while(current->next->next!=start)
        {
            /***  Checking condition for deletion of   ***/
            /*** all nodes except first and last node  ***/
            if(current->next->data==userdata)
            {
                delnode=current->next;
                current->next=current->next->next;
                free(delnode);
                return(start);
            }
            current=current->next;
        }
        if(current->next->next==start && current->next->data==userdata)
        {
            /***  Checking condition for deletion of last node  ***/
            delnode=current->next;
            free(delnode);
            current->next=start;
            return(start);
        }
    }
    printf("\nMatch not found\n");
    return(start);
}

No comments:

Post a Comment