2016-03-07 58 views
1

我的C/C++鏈接列表刪除函數不會從列表中刪除一個元素。這裏有一些我的代碼;爲什麼我的鏈接列表刪除功能不起作用?

struct listIntElement { 
    struct listIntElement *next; 
    int data; 
}; 

typedef struct listIntElement ListIntElement; 
ListIntElement *head = NULL; 

/* 
Inserts a new element infront of the list. 
*/ 
bool insert(ListIntElement **head, int data) { 

    // Allocate memory for new element. The cast is needed here as we are using a C++ compiler. 
    ListIntElement *newElement = (ListIntElement *) malloc(sizeof(ListIntElement)); 

    // Check if memory allocation was succesfull. 
    if (newElement == NULL) 
     return false; 

    // Set the data for the new element of the list. 
    newElement->data = data; 
    // Keep track of the new head of the list. 
    newElement->next = *head; 
    *head = newElement; 

    return true; 
} 

/* 
Deleting an element in the list. 
*/ 
bool remove(ListIntElement **head, ListIntElement *elementToDelete) { 
    ListIntElement *element = *head; 

    // Check for NULL pointers. 
    if (head == NULL || *head == NULL || elementToDelete == NULL) 
     return false; 

    // Special case for the head. 
    if (elementToDelete == *head) { 
     *head = element->next; 
     free(elementToDelete); 
     return true; 
    } 

    // Traversal of the list to find the element to remove. 
    while (element != NULL) { 
     if (element->next == elementToDelete) { 
      // Relink the list so that it does not include the element to be deleted. 
      element->next = elementToDelete->next; 
      free(elementToDelete); 
      return true; 
     } 
     element = element->next; 
    } 
    // elementToDelete was not found. 
    return false; 
} 

/* 
Finding an element in the list. 
*/ 
ListIntElement find(ListIntElement **head, int data) { 
    // Take care of the head as we don't want to use the head 
    // in the traversal operation. 
    ListIntElement *element = *head; 
    while (element != NULL && element->data != data) { 
     element = element->next; 
    } 
    return *element; 
} 

/* 
Displaying the list. 
*/ 
void displayList(ListIntElement **head) { 
    ListIntElement *element = *head; 

    // Check if list is empty. 
    if (head == NULL | *head == NULL) { 
     printf("List is empty\n"); 
    } 

    while (element != NULL) { 
     printf("%d --> ", element->data); 
     element = element->next; 
    } 
    printf("NULL"); 
    printf("\n"); 
} 

這是我的測試代碼;

/* 
* Testing a linked list. 
*/ 
ListIntElement found; 

printf("Linked list test\n"); 
insert(&head,0); 
insert(&head, 1); 
insert(&head, 2); 
insert(&head, 3); 
insert(&head, 4); 
insert(&head, 5); 
displayList(&head); 
printf("size is: %d\n", size(&head)); 
found = find(&head, 5); 
printf("This was found: %d\n", found.data); 
remove(&head,&found); 
displayList(&head); 

我發現此部分是刪除功能出錯的部分;

// Special case for the head. 
if (elementToDelete == *head) { 
    *head = element->next; 
    free(elementToDelete); 
    return true; 
} 

請注意,我正在使用MS Visual Studio 2015編寫C代碼並使用C++編譯器。

+2

是它未能通過簡單的沒有做任何事情,或崩潰?如果它崩潰了,你會得到什麼錯誤?另外,發佈你的'find()'函數,因爲它可以提供更多的見解。 – stellarossa

+2

爲了將來的參考,C!= C++,通常應該只標記他們正在編寫/編譯的語言(除非要求比較或對比),特別是在學習語言時。 – crashmstr

+0

@stellarossa:不,崩潰。但是,當試圖刪除列表的head元素時,if語句總是被刪除。它不應該跳過它。好的,我將添加find函數。 – PAT

回答

1

從find函數中,您返回的是副本,而不是地址本身,因此您的更改未反映在調用函數中。修復。

#include <stdio.h> 
#include <stdlib.h> 
#include <stdbool.h> 

#define true 1 
#define false 0 

struct listIntElement { 
    struct listIntElement *next; 
    int data; 
}; 

typedef struct listIntElement ListIntElement; 
ListIntElement *head = NULL; 

/* 
Inserts a new element infront of the list. 
*/ 
bool insert(ListIntElement **head, int data) { 

    // Allocate memory for new element. The cast is needed here as we are using a C++ compiler. 
    ListIntElement *newElement = (ListIntElement *) malloc(sizeof(ListIntElement)); 

    // Check if memory allocation was succesfull. 
    if (newElement == NULL) 
     return false; 

    // Set the data for the new element of the list. 
    newElement->data = data; 
    // Keep track of the new head of the list. 
    newElement->next = *head; 
    *head = newElement; 

    return true; 
} 

/* 
Deleting an element in the list. 
*/ 
bool removeElement (ListIntElement **head, ListIntElement *elementToDelete) { 
    ListIntElement *element = *head; 

    // Check for NULL pointers. 
    if (head == NULL || *head == NULL || elementToDelete == NULL) 
     return false; 

    // Special case for the head. 
    if (elementToDelete == *head) { 
     *head = element->next; 
     free(elementToDelete); 
     return true; 
    } 

    // Traversal of the list to find the element to remove. 
    while (element != NULL) { 
     if (element->next == elementToDelete) { 
      // Relink the list so that it does not include the element to be deleted. 
      element->next = elementToDelete->next; 
      free(elementToDelete); 
      return true; 
     } 
     element = element->next; 
    } 
    // elementToDelete was not found. 
    return false; 
} 


/* 
Finding an element in the list. 
*/ 
ListIntElement *find(ListIntElement **head, int data) { 
    // Take care of the head as we don't want to use the head 
    // in the traversal operation. 
    ListIntElement *element = *head; 
    while (element != NULL && element->data != data) { 
     element = element->next; 
    } 
    return element; 
} 

/* 
Displaying the list. 
*/ 
void displayList(ListIntElement **head) { 
    ListIntElement *element = *head; 

    // Check if list is empty. 
    if (head == NULL | *head == NULL) { 
     printf("List is empty\n"); 
    } 

    while (element != NULL) { 
     printf("%d --> ", element->data); 
     element = element->next; 
    } 
    printf("NULL"); 
    printf("\n"); 
} 

int main() { 
    ListIntElement *found; 

    printf("Linked list test\n"); 
    insert(&head,0); 
    insert(&head, 1); 
    insert(&head, 2); 
    insert(&head, 3); 
    insert(&head, 4); 
    insert(&head, 5); 
    displayList(&head); 
    printf("size is: %d\n", sizeof(&head)); 
    found = find(&head, 5); 
    printf("This was found: %d\n", found->data); 
    removeElement(&head,found); 
    displayList(&head); 

} 

輸出:

Linked list test 
5 --> 4 --> 3 --> 2 --> 1 --> 0 --> NULL 
size is: 4 
This was found: 5 
4 --> 3 --> 2 --> 1 --> 0 --> NULL 
+0

謝謝UnderDog。你絕對正確。我做了更改和刪除功能現在按預期工作。 :) – PAT