C 为什么我的链表删除功能不起作用?

C 为什么我的链表删除功能不起作用?,c,linked-list,C,Linked List,“我的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(L

“我的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++编译器。p> 在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);

}

它只是因为什么都不做或崩溃而失败吗?如果它崩溃了,你会犯什么错误?另外,发布您的
find()
函数,因为它可以提供更多的细节。为了将来的参考,C!=C++和一个通常只标记他们正在编写/编译的语言(除非要求比较或对比),尤其是在学习语言时。但是,当尝试删除列表的head元素时,if语句总是被跳过。它不应该跳过它。好的,我将添加find函数。是的,返回一个指针。@n.m:我明白了,谢谢大家。我将进行更改,以便find函数返回指向ListIntElement的指针,而不是ListIntElement的副本。:)谢谢你,失败者。你完全正确。我做了更改,现在删除功能按预期工作。:)
Linked list test
5 --> 4 --> 3 --> 2 --> 1 --> 0 --> NULL
size is: 4
This was found: 5
4 --> 3 --> 2 --> 1 --> 0 --> NULL