Tuesday, 12 July 2011

Insert After..

void ins_aft(node *start)
{
    int userdata;                    /* Data for inserting a node*/
    int flag=0;
    node *newnode;              /* New inputed node*/
    node *current;              /* Node for travelling the linked list*/
    newnode=(node*)malloc(sizeof(node));
    printf("\nEnter the Data after which you want to insert a node\n");
    scanf("%d",&userdata);
    current=start;
    while(current->next!=start)
    {
        /***  Insertion checking for all nodes except last  ***/
        if(current->data==userdata)
        {
            newnode->next=current->next;
            current->next=newnode;
            flag=1;
        }
        current=current->next;
    }
    if(flag==0 && current->next==start && current->data==userdata)
    {
        /***  Insertion checking for last nodes  ***/
        newnode->next=current->next;    /** start is copied in newnode->next**/
        current->next=newnode;
        flag=1;
    }
    if(flag==0 && current->next==start)
        printf("\nNo match found\n");
}

No comments:

Post a Comment