create link list

Discussion in 'C' started by answerme, Mar 24, 2008.

  1. answerme

    answerme New Member

    Joined:
    Dec 17, 2007
    Messages:
    114
    Likes Received:
    0
    Trophy Points:
    0
    Hi
    This prg is for creating a link list .But Iam not able to understand what will last line do q->link=tmp;

    Code:
    int create(int data)
    {
    	struct node *q,*tmp;
    	
    	tmp=malloc(sizeof(struct node));
    	tmp->info=data;
    	tmp->link=NULL;
    	
    	if(start==NULL)
    		start=tmp;
    	else
    	{	
    		q=start;
    		while(q->link!=NULL)
    		{
    			q=q->link;
    		}
    		
    			[B]q->link=tmp;[/B] // why this is required
    	}
    }
    
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    It will assign the last node ( the one which is being created ) at the end of the q
     
  3. lead.smart34

    lead.smart34 New Member

    Joined:
    Feb 14, 2008
    Messages:
    77
    Likes Received:
    0
    Trophy Points:
    0
    in temp you are taking the data to be inserted in the linked list .

    so you are attaching that node as the next node of q here
     

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