James Green
James Green

Reputation: 125

adding to tail of linked list of pointers to structs

I'm basing this off of code that appends to a linked list of structs, and I've now moved to using pointers of structs for a new project, and I can't get the code to work, there is no output. When I run it through debug mode I find that my method of appending to the end of the linked list isn't working.

Here are my structs I use a struct for holding the linked list because it's better than using a head pointer on its own.

typedef struct competitor competitor;
typedef struct ll_competitor ll_competitor;

struct competitor {
    int data;
    competitor *next;
};

struct ll_competitor {
    struct competitor *head;
    int size;
};

This just makes a pointer to a struct, stores the passed data and returns it

competitor* make_competitor(int data){
    competitor *competitor_ptr;
    competitor_ptr = malloc(sizeof(competitor));

    if (competitor_ptr != NULL){ // if malloc was successful
        competitor_ptr->data = data;
        competitor_ptr->next = NULL;
    }
    return competitor_ptr;
}

This should add an item to the end of the linked list

void insert_tail_competitor(ll_competitor *ll, competitor *insertion){
    competitor *temp_pointer;
    temp_pointer = ll->head;

    while (temp_pointer != NULL){ // while not null aka until the end of this chain
        temp_pointer = (temp_pointer)->next;
    }
    temp_pointer = insertion;
}

This should iterate over the linked list, outputting the data

void print_all_competitors(ll_competitor *ll){
    printf("Print all competitors");
    competitor *temp_ptr = ll->head;
    int counter = 0;
    while (temp_ptr != NULL){
        printf("\nCount: %d\n", counter++);
        printf("data: %d",temp_ptr->data);
        temp_ptr = temp_ptr->next;
    }
}

Builds the linked list for me

ll_competitor *make_competitor_ll(){
    ll_competitor *ll = malloc(sizeof *ll); // do a linked list builder later
    ll->head=NULL; ll->size=0;
    return ll;
}

Main initialises 5 pointers to competitors using the make_competitor function, I expect an output of 1 2 3 4 5

int main() {

    ll_competitor *ll = make_competitor_ll();

    competitor* c1 = make_competitor(1);
    competitor* c2 = make_competitor(2);
    competitor* c3 = make_competitor(3);
    competitor* c4 = make_competitor(4);
    competitor* c5 = make_competitor(5);


    insert_tail_competitor(ll,c1);
    insert_tail_competitor(ll,c2);
    insert_tail_competitor(ll,c3);
    insert_tail_competitor(ll,c4);
    insert_tail_competitor(ll,c5);

    print_all_competitors(ll);
    return 0;
}

EDIT: To clarify my confusion, here is the code I based this off of

typedef struct node_tag *node_pointer;

typedef struct node_tag{
    int data;
    node_pointer next;
}node;

void insert_at_tail(node_pointer *pointer_to_head, node_pointer new_node_pointer){
    node_pointer *temp_pointer;
    temp_pointer = pointer_to_head; 
    while (*temp_pointer != NULL){
        temp_pointer = &(*temp_pointer)->next;
    }
    new_node_pointer->next = *temp_pointer;
    *temp_pointer = new_node_pointer;
}

Upvotes: 0

Views: 206

Answers (1)

David Grayson
David Grayson

Reputation: 87486

The only thing insert_tail_competitor does is define a local variable named temp_pointer and then change that pointer several times, so we would not expect that function to have any lasting effect on the data in your program.

If you want to add an entry to your linked list, you actually need to modify the next pointer of a struct.

Maybe this would work:

void insert_tail_competitor(ll_competitor * ll, competitor * insertion)
{
  competitor *  c = ll->head;
  if (c == NULL)
  {
    ll->head = insertion; 
    return;
  }

  while (c->next != NULL) { c = c->next; }
  c->next = insertion;
}

(By the way, for more maintainable code, you'd probably want to find some way to handle the special c == NULL case the same way as the other cases, but let's keep it simple for now.)

Upvotes: 2

Related Questions