Adding a node to linked list

This is a discussion on Adding a node to linked list within the C Programming forums, part of the General Programming Boards category; I have a function that is supposed to add a node to a linked list. I'm not sure what the ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    15

    Adding a node to linked list

    I have a function that is supposed to add a node to a linked list. I'm not sure what the other 2 parts of the for loop are for this. Can anyone help me figure those out? Also, is everything else I have right? Linked lists are pretty new to me so i may have some other mistakes.


    Code:
    void add_node(info_node **headp, char job_title[25], double wage)
    
    
    {
    	
    	info_node *current_node;
    	
    	if (*headp==NULL)
    	{
    		*headp=(info_node *)malloc(sizeof(info_node));
    		(*headp)->linkp==NULL;
    	}
    	else 
            {
    		for(????;?????;current_node->linkp==NULL)
    		{
    			current_node->linkp=(info_node *)malloc(sizeof(info_node));
                            current_node->linkp->linkp->job_title=job_title;
    			current_node->linkp->linkp->hourly_wage=wage; 
    		}	
    		
    	}
    	
    	
    	
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,743
    There are lots of things wrong.

    If you want to walk a linked list all the way through, you write this:

    Code:
    for ( current_node = *headp; current_node != NULL; current_node = current_node->next )
    Of course, the body of the loop comes later, and certain parts may change, depending on the structure of the list, such as tailp being the tail rather than NULL.

    If you want to add a node to the tail end of the list, you have to move to the last node first. Then you point to the new node in that next pointer, and make the new node's next pointer NULL.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list statement 4 adding a node
    By llinocoe in forum C Programming
    Replies: 5
    Last Post: 09-28-2008, 09:52 AM
  2. Replies: 0
    Last Post: 09-16-2008, 05:04 AM
  3. Adding a node to a linked list
    By slopeski007 in forum C Programming
    Replies: 2
    Last Post: 02-01-2003, 11:31 PM
  4. Adding Node - Double Linked List
    By SeanMSimonsen in forum C++ Programming
    Replies: 3
    Last Post: 11-10-2002, 11:43 AM
  5. Replies: 5
    Last Post: 10-04-2001, 03:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21