2014-11-22 94 views
0

我正在编写一个程序,它将字符串保存到链接列表中,同时为字符串和节点分配内存。我有我的插入和搜索功能完美工作,但我似乎无法让我的删除功能工作。它似乎并没有从节点中删除信息,但我对输出什么和释放什么感到不知所措。即使只是一个暗示,任何帮助都会受到欢迎。从动态链接列表中删除节点

我的节点和目录结构

typedef struct listNode {    //simple linked list structure 
struct listNode *next;     //address to next 
char *data;       //data 
} NODE; 

typedef struct ListStruct { 
    NODE *head;       //head node for iterating 
} LIST; 

这是我目前在删除节点

void deleteNode(LIST *list, char *string){   // passed linked list and string to find 
NODE *prev, *curr, *temp;      //variables init 
//int compare;        // for strcmp if needed 
prev = NULL;        //set prev to null 
curr = list->head;       //set current to the head of the list 
while(curr != NULL){       //while the current node is not null 
if(strcmp(curr->data,string) == 0){   //check for the proper string 
    temp = curr;       //set temp to current node to be deleted 
    temp->data = strcpy(curr->data);  //copy data so free is possible 
    prev->next = temp;      //set the prev to temp 
    free(curr->data);      //free malloc'd data 
    free(curr);       //free malloc'd node 
    curr = temp;       //set curr back to temp 
} 
else{        //if string isn't found at current 
    prev = curr;      //set previous to current 
    curr = curr->next;     //and current to current.next 
} 

} 
}//done 

的非工作版本,我知道,当我找到正确的琴弦误差,但我不能为我的生活找出什么是错的。希望尽快听到别人的声音,并一如既往地感谢你。

+0

我会建议使功能更模块化。将搜索和比较部分表示为一个功能,它将返回一个NODE。 然后,删除只会将NODE作为参数,并释放它的字符串和NODE本身。 通过这样使其更具可调试性,可维护性和易于理解。 – Maor 2014-11-22 01:17:21

回答

3

您可能需要更新,如果阻止一点点:

if(strcmp(curr->data,string) == 0){   //check for the proper string 
    temp = curr;       //set temp to current node to be deleted 
    if (prev == NULL)       // if the first node is the one to be deleted 
    list->head = curr->next; 
    else 
    prev->next = curr->next;    //set prev next pointer to curr next node 
    curr = curr->next;      //curr updated 
    free(temp->data);      //free malloc'd data 
    free(temp);       //free malloc'd node 

    break; //assume there is only one unique string in the link list 
} 
+0

一个更正:它应该是'list-> head'而不是'list-> header'。 – 2014-11-22 01:28:25

+0

@striving_coder,谢谢!更新。 – Dere0405 2014-11-22 01:29:39

+0

仍然没有工作,但我会与这个和一些printf的工作,看看我能否让她工作。感谢您的帮助。 – Chris 2014-11-22 01:31:27

0

以下应该工作。我们需要确保当前节点是第一个节点时,列表的头部应该指向下一个节点。如果多个节点具有与输入字符串相同的数据,它也可以工作。

void deleteNode(LIST *list, char *string) 
{           // passed linked list and string to find 
NODE *prev, *curr;       //variables init 
prev = NULL;        //set prev to null 
curr = list->head;       //set current to the head of the list 

while(curr != NULL){      //while the current node is not null 
if(strcmp(curr->data,string) == 0){   //compare the given string with node data 
    temp = curr; 
    if (curr == list->head)     //if first node is to be deleted point 
     list->head = curr->next;   //head of list to next node first then 
    else             
     prev->next =curr->next;   //point prev node's next to current's next 
    curr=curr->next; 

    free(temp->data);      //free malloc'd data 
    free(temp);       //free malloc'd node 
} 
else{         //if string isn't found at current 
    prev = curr;       //set previous to current 
    curr = curr->next;      //and current to current.next 
} 

}//while 
}//void deleteNode