Monday, 11 July 2011

Insert Before..

node* ins_bef(node *current)
{
    int userdata;         
    node *newnode,*temp;
    newnode=(node*)malloc(sizeof(node));
    printf("\nEnter the data before which you want to insert a node\n");
    scanf("%d",&userdata);
    init(newnode);
    if(current->data==userdata)
    {
        /*** Insertion checking for first node ***/
        newnode->next=current;
        current->prev=newnode;
        current=newnode;
        return(current);
    }
    temp=current;
    while(temp->next!=NULL)
    {                                       
        /*** Insertion checking for all node except first ***/
        if(temp->next->data==userdata)
        {
            newnode->next=temp->next;
            temp->next->prev=newnode;
            temp->next=newnode;
            newnode->prev=temp;
            return(current);
        }
        temp=temp->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(current);
}

No comments:

Post a Comment