Insert a node in a Linked List

Discussion in 'C' started by shabbir, Aug 27, 2006.

  1. Insert a node before a particular node in a linked ist
    Code:
    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:
    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:
    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:
    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:
    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
     
  2. rashida.par

    rashida.par New Member

    Joined:
    Feb 14, 2008
    Messages:
    21
    Likes Received:
    1
    Trophy Points:
    0
    ok understood
     
  3. programming girl

    programming girl New Member

    Joined:
    Apr 24, 2008
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Your Forum truly wonderful, I benefited from the subject a lots information :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice