2016-02-04 145 views
-1

我试图在另一个链接列表struct node的每个节点内实现struct node_delay_info的链接列表。链接列表中的链接列表:无法检索值

我在两个函数中扫描列表: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分配的内存,但我不认为这是问题。

+0

您是否尝试使用调试器来逐步执行代码? –

+1

请一直显示您的研究成果。请先阅读[问]页面。 –

+0

@SouravGhosh我用'valgrind'输出编辑了问题。 我会马上阅读如何提出并改进我的问题。 – elmazzun

回答

0

在将新元素附加到列表末尾后,原始代码缺少一行来更新尾指针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; 
      } 
     }