2014-11-22 69 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