C语言 链表中的链表:无法检索值



我正在尝试在另一个链表的每个节点内实现struct node_delay_info的链表struct node

我通过两个功能扫描列表:callback_function()分配和填充列表,study_list()打印struct node_delay_info的值。

虽然我成功地用callback_function()填充了所有字段,但对study_list()中列表的另一次扫描不会打印我在上一个函数中插入的值。

代码如下:

typedef struct node_delay_info {
    double delay_info;
    struct node_delay_info *next;
} node_delay_info;
typedef struct node {
    /* bunch of other data */
    ...
    /* useful when reading values from the head list */
    struct node_delay_info *first_elem;
    /* useful when inserting new nodes at bottom list */
    struct node_delay_info *last_elem;
    struct node *next;
} node;
node *head = NULL;
/* malloc return values are not checked here for brevity */
void callback_function(/* args */) {
    node *tmp = NULL;
    if (head == NULL) { // if list is empty
        node *new_node = (node *)malloc(sizeof(node));
        node_delay_info *delay_node = (node_delay_info *)malloc(sizeof(node_delay_info));
        new_node->next = NULL;
        delay_node->delay_info = 0.0;
        delay_node->next = NULL;
        new_node->first_elem = delay_node;
        new_node->last_elem = delay_node;
        head = new_node;   
    } else { // if list is not empty
        tmp = head;
        while (tmp->next != NULL) {
            if (/*the node of the list has some matching values for me*/) {
                /* calculating stuff */
                unsigned long delay = ...;
                /* updating the delay_field value */
                node_delay_info *d_info = (node_delay_info *)malloc(sizeof(node_delay_info));
                d_info->delay_info = (double)delay;
                d_info->next = NULL;
                tmp->last_elem->next = d_info;
                /* I checked the value of delay_info field inside d_info struct 
                and it is successfully filled with delay */
                return; // I no longer need to search
            } else {
                /* the node of the list has no matching value, must go forward */
                tmp = tmp->next;
            }
        }
        /* if we are here we are at the end of the list and no element 
        of it matches my values, so I allocate a new node  */        
        node *new_node = (node *)malloc(sizeof(node));
        new_node->next = NULL;
        node_delay_info *delay_node = (node_delay_info *)malloc(sizeof(node_delay_info));
        delay_node->delay_info = 0.0;
        delay_node->next = NULL;
        new_node->first_elem = delay_node;
        new_node->last_elem = delay_node;
        tmp->next = new_node;
    }
}
void study_list() {
    node *temp = head;
    node_delay_info *info_temp2;
    while (temp->next != NULL) {
        info_temp2 = temp->first_elem;
        while (info_temp2->next != NULL) {
            printf("%lf -> ", info_temp2->delay_info);
            info_temp2 = info_temp2->next;
        }
        printf("n");
        temp = temp->next;
    }
}
int main() {
    ...
    /* variables and structs for libpcap */
    pcap_loop(descr, how_many_pkts, my_callback, NULL);
    study_list();
}

输出:

0.000000 ->

0.000000 ->

0.000000 ->

编辑 1:运行 ~$sudo valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./main 显示0.000000 -> 的输出,然后让我想起x bytes in y blocks are still reachable in loss record:我想它指的是分配了我没有释放的各种malloc的内存,但我认为这不是问题所在。

原始代码缺少一行,用于在将新元素附加到列表末尾后更新尾指针tmp->last_elem。 请参阅下面标记为// <-- This line was missing!的行:

        tmp = head;
        while (tmp->next != NULL) {
            if (/*the node of the list has some matching values for me*/) {
                /* calculating stuff */
                unsigned long delay = ...;
                /* updating the delay_field value */
                node_delay_info *d_info = (node_delay_info *)malloc(sizeof(node_delay_info));
                d_info->delay_info = (double)delay;
                d_info->next = NULL;
                tmp->last_elem->next = d_info;
                tmp->last_elem = d_info; // <-- This line was missing!
                /* I checked the value of delay_info field inside d_info struct 
                and it is successfully filled with delay */
                return; // I no longer need to search
            } else {
                /* the node of the list has no matching value, must go forward */
                tmp = tmp->next;
            }
        }

相关内容

  • 没有找到相关文章

最新更新