Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Insert a node in a linked list (http://www.go4expert.com/articles/insert-node-linked-list-t1278/)

shabbir 27Aug2006 18:02

Insert a node in a linked list
 
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 13Mar2008 15:38

Re: Insert a node in a linked list
 
ok understood

programming girl 25Apr2008 23:44

Re: Insert a node in a linked list
 
Your Forum truly wonderful, I benefited from the subject a lots information :)


All times are GMT +5.5. The time now is 12:28.