2013-07-15 52 views
-2

我有一个列表结构的代码和实现它的代码。实现一个列表C

结构,entry_t是名单上的数据的类型:

#ifndef _list_private_h 
    #define _list_private_h 

    typedef struct list_t{ 
     struct node_t *head; 
     int size; 
    }; 

    typedef struct node_t{ 
     struct entry_t *element; 
     struct node_t *next; 
    }node_t; 

    #endif 

代码:

struct list_t *list_create(){ 
     struct list_t *list = (struct list_t*) malloc(sizeof(struct list_t)); 
     list->head=NULL; 
     list->size=0; 
     return list; 
} 

int list_destroy(struct list_t *list){ 
     node_t *no = list->head; 
     while(no!=NULL){ 
       node_t *aux=no; 
       entry_destroy(no->element); 
       no=no->next;    
       free(aux);   
       list->size=(list->size)-1;     
     } 
     free(list); 
     return 0; 
} 

int list_add(struct list_t *list, struct entry_t *entry){ 
     node_t *no = list->head; 
     if(no==NULL){ 
      list->head=(node_t*) malloc(sizeof(node_t)); 
      list->head->element=entry_dup(entry); 
      list->size=list->size+1; 
      return 0; 
     } 
     else{ 
      while(no!=NULL){ 
        no=no->next; 
      } 
      no=(node_t*) malloc(sizeof(node_t)); 
      no->element=entry_dup(entry); 
      list->size=list->size+1; 
      return 0; 
     } 
     return -1; 
    }   


struct entry_t *list_get(struct list_t *list, char *key){ 
     node_t *no = list->head; 
     while(no!=NULL){    
       if(strcmp(no->element->key,key)==0){ 
        return no->element;    
       } 
       no=no->next; 
     } 
     return NULL; 
    } 

当我运行这些测试并不元素添加到列表中:

int testEmptyList() { 
     struct list_t *list = list_create(); 
     int result = list != NULL && list_size(list) == 0; 
     list_destroy(list); 
     printf("Test empty list: %s\n",result?"pass":"not pass"); 
     return result; 
} 

int testAddHead() { 
     int result; 
     struct list_t *list = list_create(); 
     struct entry_t *entry = entry_create(strdup("abc"),data_create(5)); 
     memcpy(entry->value->data,"abc1",5); 
     list_add(list,entry); 
     result = list_get(list,"abc") == entry && 
     list_size(list) == 1; 
     list_destroy(list); 
     printf("Module list -> test add first: %s\n",result?"pass":"not pass"); 
     return result; 
} 

所以,我想要的是把这个代码添加到列表中的元素。谢谢。

+4

不投下malloc的退货 – Alexis

+1

“list_add”的代码在哪里? – Joni

+2

我不介意人们是否想投票,但除非他们被告知为什么他们被拒绝投票,否则没有人从投票中学到东西。 – Kaganar

回答

1

试试这个:

int list_add(struct list_t *list, struct entry_t *entry){ 
     node_t *no = list->head; 
     if(no==NULL){ 
      list->head=(node_t*) malloc(sizeof(node_t)); 
      list->head->element=entry_dup(entry); 
      list->size=list->size+1; 
      return 0; 
     } 
     else{ 
      while(no->next!=NULL){ 
      no=no->next; 
      } 
     no->next=(node_t*) malloc(sizeof(node_t)); 
     no->next->element=entry_dup(entry); 
     no->next->next = NULL; 
     list->size=list->size+1; 
     return 0; 
     } 
    return -1; 
    } 

的问题是,前一个节点需要知道下一个的地址,通过指针next。在你的情况下,no->next将等于NULL(在循环之后),所以它是最后一个节点。您从不将最后一个节点的next指针指定给新节点,因此它将丢失。

1

几个问题:

  • 您通过list_destroy破坏列表,它可以在进入呼叫entry_destroy添加到列表中调用list_get返回一个指针(不是副本)的条目之前。
  • list_add您可以拨打malloc为新节点分配空间,但是您不要将其元素设置为NULL。由于malloc不保证已分配的内存已被擦除,因此该列表永远不会以其next元素设置为NULL的节点导致虚假结果。
  • else分公司list_add保证noNULL(或程序会从一个segfault前面给出的问题坠毁。)你可能想终止时no->nextNULL,而不是当noNULL。此外,该分支需要明确指定next元素为NULL