Monday, 11 July 2011

Insert After..

void ins_aft(node *current)
{
   
node *newnode;
int userdata;
    newnode=(node*)malloc(sizeof(node));
    printf("\nEnter the data after which you want to insert a node\n");
    scanf("%d",&userdata);
    init(newnode);
    while(current->next!=NULL)
    {
        /***  Insertion checking for all nodes except last  ***/
        if(current->data==userdata)
        {
            newnode->next=current->next;
            current->next->prev=newnode;
            current->next=newnode;
            newnode->prev=current;
            flag=1;
        }
        current=current->next;
    }
    if(flag==0 && current->next==NULL && current->data==userdata)
    {
        /***  Insertion checking for last nodes  ***/
        newnode->next=current->next;
        current->next=newnode;
        flag=1;
    }
    else if(flag==0 && current->next==NULL)
        printf("\nNo match found\n");

}

No comments:

Post a Comment