C语言中如何检查链表、动态数据结构中是否存在人?

问题描述 投票:0回答:1

这是头文件中的代码:

typedef struct Human {                  
    char name[50];
    char surname[50];
    struct Human *mother;
    struct Human *father;
    struct Human *next;
} Human;

void addHuman(List *list, char *name, char *surname, Human *father, Human *mother);
Human *findHuman(List *list, char *name, char *surname);


以下代码来自源文件,它不起作用:

void addHuman(List *list, char *name, char *surname, Human *father, Human *mother);


    // Check if the list is NULL
    if (list == NULL) {
        printf("Error: List is NULL\n");
        return;
    }
    // Check if the person already exists in the list
    Human *existingHuman = findHuman(list, name, surname);
    if (existingHuman != NULL) {
    printf("Person exists already.\n");
    return;
    }

    // Create new person
    Human *new = (Human *) malloc(sizeof(Human));
    strcpy(new->name, name); //strcpy:copy strings
    strcpy(new->surname, surname);
    new->mother = mother;
    new->father = father;

我该如何修复它?

感谢您的帮助!

我尝试检查全局变量和局部变量是否匹配。`

c data-structures dynamic linked-list
1个回答
0
投票

要检查某人(或元素)是否在链表中,您必须遍历链表并将每个元素与您要查找的元素进行比较。以下是在 C 中执行此操作的方法:

#include <stdio.h>
#include <stdbool.h> // Include the header for boolean data type

bool personExists(const char* name, const char* surname) {
    Human* current = head; // Start from the beginning of the list

    while (current != NULL) {
        // Compare the name and surname with the current element
        if (strcmp(current->name, name) == 0 && strcmp(current->surname, surname) == 0) {
            return true; // Person found in the list
        }
        current = current->next; // Move to the next element
    }

    return false; // Person not found in the list
}

您可以在代码中调用此 personExists 函数来检查链接列表中是否存在人员:

if (personExists("John", "Doe")) {
    printf("Person exists in the list.\n");
} else {
    printf("Person does not exist in the list.\n");
}

如果源文件中的 addHuman 函数不起作用,则这应该可以起作用(合并建议):

// Include necessary headers
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Define the Human structure
typedef struct Human {
    char name[50];
    char surname[50];
    struct Human* mother;
    struct Human* father;
    struct Human* next;
} Human;

// Define the List structure if not already defined
typedef struct List {
    // Define the list structure here
    // You can include a pointer to the first Human in the list, for example
    Human* head;
} List;

// Function prototypes
void addHuman(List* list, char* name, char* surname, Human* father, Human* mother);
Human* findHuman(List* list, char* name, char* surname);

// Implementation of addHuman function
void addHuman(List* list, char* name, char* surname, Human* father, Human* mother) {
    // Check if the list is NULL
    if (list == NULL) {
        printf("Error: List is NULL\n");
        return;
    }

    // Check if the person already exists in the list
    Human* existingHuman = findHuman(list, name, surname);
    if (existingHuman != NULL) {
        printf("Person exists already.\n");
        return;
    }

    // Create new person
    Human* new = (Human*)malloc(sizeof(Human));
    if (new == NULL) {
        printf("Error: Memory allocation failed\n");
        return;
    }

    strcpy(new->name, name);
    strcpy(new->surname, surname);
    new->mother = mother;
    new->father = father;

    // Add new person to the list
    new->next = list->head;
    list->head = new;
}

// Implementation of findHuman function (you need to define this function)

int main() {
    // Initialize your list
    List myList;
    myList.head = NULL;

    // Example usage:
    addHuman(&myList, "John", "Doe", NULL, NULL);

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.