Tuesday, 12 July 2011

Insert Before..

node* ins_bef(node *start)
{
    int userdata;                    /* Data for inserting a node*/
    node *newnode;              /* New inputed node*/
    node *current;              /* Node for travelling the linked list*/
    newnode=(node*)malloc(sizeof(node));
    current=start;
    printf("\nEnter the data before which you want to insert a node\n");
    scanf("%d",&userdata);
    if(current->data==userdata)
    {
        newnode->next=start;
        while(current->next!=start)
        current=current->next;
        current->next=newnode;
        start=newnode;
        return(start);
    }
    while(current->next!=start)
    {
        if(current->next->data==userdata)
        {
            newnode->next=current->next;
            current->next=newnode;
            return(start);
        }
        current=current->next;
    }
    /*
    If the function does not return from any return statement.
    There is no match to insert before the input  roll number.
    */

    printf("\nMatch not found\n");
    return(start);
}

No comments:

Post a Comment