添加到结构指针链表的尾部

adding to tail of linked list of pointers to structs

我基于附加到结构链接列表的代码,现在我已经开始使用结构指针进行新项目,但我无法让代码工作,那里是没有输出。当我 运行 通过调试模式时,我发现我的追加到链表末尾的方法不起作用。

这是我的结构 我使用一个结构来保存链表,因为它比单独使用一个头指针要好。

typedef struct competitor competitor;
typedef struct ll_competitor ll_competitor;

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

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

这只是指向结构的指针,存储传递的数据并returns它

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;
}

这个应该在链表的末尾添加一个项目

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;
}

这应该遍历链表,输出数据

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;
    }
}

为我构建链表

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 使用 make_competitor 函数初始化 5 个指向竞争对手的指针,我期望输出为 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;
}

编辑:为了澄清我的困惑,这是我基于

的代码
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;
}

insert_tail_competitor 唯一做的就是定义一个名为 temp_pointer 的局部变量,然后多次更改该指针,因此我们不希望该函数对您的数据产生任何持久影响程序。

如果你想在你的链表中添加一个条目,你实际上需要修改一个结构体的next指针。

也许这行得通:

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;
}

(顺便说一下,对于更易于维护的代码,您可能希望找到某种方法来处理特殊的 c == NULL 情况,就像处理其他情况一样,但现在让我们保持简单。 )