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

时间:2016-03-07 19:18:47

标签: c linked-list

我的C / C ++链接列表删除功能不会从列表中删除元素。以下是我的一些代码;

int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USB_DEVICE_Init();

  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */
      CDC_Transmit_FS(buf, 300);
      HAL_Delay(1);
  /* USER CODE BEGIN 3 */

  }
  /* USER CODE END 3 */

}

这是我的测试代码;

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);

请注意我使用MS Visual Studio 2015编写C代码并使用C ++编译器。

1 个答案:

答案 0 :(得分:1)

从find函数中,您返回的是副本,而不是地址本身,因此您的更改未反映在调用函数中。修正了它。

public void run() {
    if(timeToShutdown) return;
    doA();
    if(timeToShutdown) return;
    doB();
    /*etc*/
}

输出:

#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);

}