Insert a node in a linked list

shabbir's Avatar author of Insert a node in a linked list
This is an article on Insert a node in a linked list in C.
Insert a node before a particular node in a linked ist
Code: C
node* ins_bef(node *current)
{
    int rno;                         /* Roll number for inserting a node*/
    node *newnode,*temp;
    newnode=(node*)malloc(sizeof(node));
    printf("\nEnter the roll number before which you want to insert a node\n");
    scanf("%d",&rno);
    init(newnode);
    if(current->roll_no==rno)
    {
        newnode->next=current;
        current=newnode;
        return(current);
    }
    temp=current;
    while(temp->next!=NULL)
    {
        if(temp->next->roll_no==rno)
        {
            newnode->next=temp->next;
            temp->next=newnode;
            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);
}
You should be calling the above function as follows
Code: C
head=ins_bef(head);// Head is the first node of the linked list
 
Insert a node after a particular node in a linked list
Code: C
void ins_aft(node *current)
{
    int rno;                         /* Roll number for inserting a node*/
    int flag=0;
    node *newnode;
    newnode=(node*)malloc(sizeof(node));
    printf("\nEnter the roll number after which you want to insert a node\n");
    scanf("%d",&rno);
    init(newnode);
    while(current->next!=NULL)
    {
        /***  Insertion checking for all nodes except last  ***/
        if(current->roll_no==rno)
        {
            newnode->next=current->next;
            current->next=newnode;
            flag=1;
        }
        current=current->next;
    }
    if(flag==0 && current->next==NULL && current->roll_no==rno)
    {
        /***  Insertion checking for last nodes  ***/
        newnode->next=current->next;
        current->next=newnode;
        flag=1;
    }
    if(flag==0 && current->next==NULL)
        printf("\nNo match found\n");
}
You should be calling the above function as follows
Code: C
ins_aft(head); // Head is the first node of the linked list
 
And last but not the least you should also know how the structure of the linked list is defined.
Code: C
struct list{
    int roll_no;          /* Storing roll number of a node */
    char name[N];         /* Storing name of node */
    float marks;          /* Storing marks of a node */
    struct list *next;    /* Storing next address */
};
/*****  Redefining struct list as node  *****/
typedef struct list node;
The difference in the return type of ins_bef and ins_aft is because inserting before can be done for the first node as well and so we may need to update the head but thats not the case with inserting after
rashida.par's Avatar, Join Date: Feb 2008
Go4Expert Member
ok understood
programming girl's Avatar, Join Date: Apr 2008
Light Poster
Your Forum truly wonderful, I benefited from the subject a lots information