Jump to content

Help with inserting into a linked list (C)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
Mt570

Mt570

    Newbie

  • Members
  • Pip
  • 3 posts
Hey I have to write a C program that is a linked list of customer data, with various functions.
The function I'm having trouble with is the insertAtIndex function, this function needs to insert a new customer data at the location indicated by the parameter "index". If index is 0 then it should be inserted as the first data in the linked list.

I can add one new element fine but when I try to add another the program crashes. Heres the code:

struct Contact
{
  char  * last_name;
  char * first_name;
  int id;
  float balance;
  struct Contact *next;
  
};

struct Contact * head = NULL;



int insertAtIndex(char * lastname, char * firstname, int id, float balance, int index)
{
    int i;
    
    struct Contact * newContact, *conductor;
    conductor = head;
    
    newContact = (struct Contact *) malloc(sizeof(struct Contact));
    
    strcpy(newContact->last_name,lastname);
    strcpy(newContact->first_name,firstname);
    newContact->id = id;
    newContact->balance = balance;
    
    newContact->next = NULL;
    
    if(conductor == NULL) 
    { 

      head=newContact; 
      newContact->next = NULL; 
    }
    
    else
    {
    
        
      for(i = 0; i< index; i++)
      {
       conductor=conductor->next;
       
      }           
      conductor->next=newContact;

    }
        
    return index;
}

Heres two more functions that is supposed to do the same thing as the one above but has the same problem:

int insertAtIndex(char * lastname, char * firstname, int id, float balance, int index)
{

 int i;
    
    struct Contact * newContact, *conductor;
    conductor = head;
    
    newContact = (struct Contact *) malloc(sizeof(struct Contact));
    lastname = (char *)malloc(50 * sizeof(char));
    firstname = (char *)malloc(50 * sizeof(char));
    
    strcpy(newContact->last_name,lastname);
    strcpy(newContact->first_name,firstname);
    newContact->id = id;
    newContact->balance = balance;
    
    newContact->next = NULL;
    
    if(conductor == NULL) 
    { 

      head=newContact; 
      newContact->next = NULL; 
    }
    
    else
    {
    
        
      for( i = 0; conductor && i < index; i++, conductor = conductor->next )
      {
           if( conductor->next == NULL )
            break;
      }

      newContact->next = conductor->next;
      conductor->next = newContact;
    }
        
   
    return index;
}

and

int insertAtIndex(char * lastname, char * firstname, int id, float balance, int index)
{

  struct Contact *temp,*r;
    int i;
    temp=head;

        r=(struct Contact*)malloc(sizeof(struct Contact));
        lastname = (char *)malloc(50 * sizeof(char));
        firstname = (char *)malloc(50 * sizeof(char));
       
        strcpy(r->last_name,lastname);
        strcpy(r->first_name,firstname);
        r->id = id;
        r->balance = balance;

    if(head == NULL) 
    { 

      head=r; 
      r->next = NULL; 
    }
    
    else
    {
        for(i=0;i<index;i++)
        temp=temp->next;
    
        if(temp==NULL)
        {
            printf("there are less than %d elements in list\n",index);
            return -1;
        }

        r->next=temp->next;
        temp->next=r;
    }
        return index;
}

I'm not sure what I'm doing wrong.

I can post my whole code with the main function if need to be. Any help would be appreciated.

Edited by Mt570, 26 September 2009 - 07:33 PM.


#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
      for(i = 0; i< index; i++)
      {
       conductor=conductor->next;
       
      }  

You allocate your conductor, and then set conductor to conductor->next, which is null. Now you try to access conductor->next, you reference a null pointer, and boom. Fail. You need to put your allocation in your loop.
sudo rm -rf /

#3
Mt570

Mt570

    Newbie

  • Members
  • Pip
  • 3 posts
Ok thanks I got it working

#4
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
you don't need to cast malloc in C.

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
Some compilers force you to, others warn you not to.
sudo rm -rf /

#6
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

dargueta said:

Some compilers force you to, others warn you not to.
C++ compilers force the cast; C compilers shouldn't (or they're broken).

#7
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
That would explain a lot of things, actually.
sudo rm -rf /