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

00jrzges  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(119)

这是头文件中的代码:

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;


我怎么才能修好它?
谢谢你的帮助!
我试着检查全局变量和局部变量是否匹配。

6vl6ewon

6vl6ewon1#

要检查某个人(或某个元素)是否在链表中,你必须遍历链表,并将每个元素与你要查找的内容进行比较。下面是如何在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
}

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

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

qvsjd97n

qvsjd97n2#

我还为您的问题提供了一个可能的解决方案,并且与最初提交的答案类似。基本前提是设置某种类型的初始“Human”结构,然后在创建新条目时通读列表。由于没有最低代码,这给了我一点艺术许可证来提供一个代码集,以供思考。以下是原型代码。

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

#define SIZE 50

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

Human * add_parent(char * first, char * last)
{
    Human * work = malloc(sizeof(Human));
    strcpy(work->name, first);
    strcpy(work->surname, last);
    work->mother = NULL;
    work->father = NULL;
    work->next   = NULL;

    return work;
}

Human * findHuman(Human *ls, char *name, char *surname)
{
    Human * work = ls;

    while(1)
    {
        if ((strcmp(work->name, name) == 0) && strcmp(work->surname, surname) == 0)
            return work;
        if (work->next == NULL)
            break;
        work = work->next;
    }

    return NULL;
}

void addHuman(Human *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 (findHuman(list, name, surname) != NULL)
    {
        printf("Person exists already.\n");
        return;
    }

    while(1)
    {
        if (list->next == NULL)
            break;
        list = list->next;
    }

    // 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;
    new->next   = NULL;

    list->next = new;

    return;
}

int main()
{
    Human * Dad = NULL;
    Human * Mom = NULL;
    Human * Lst = NULL;

    char f_name[SIZE];
    char l_name[SIZE];

    Mom = add_parent("Jane", "Smith");          /* Some initial parents */
    Dad = add_parent("John", "Smith");

    Mom->next = Dad;

    printf("Mom: %s %s\n", Mom->name, Mom->surname);
    printf("Dad: %s %s\n", Mom->next->name, Mom->next->surname);

    while(1)
    {
        printf("Enter first name or \"quit\" to end entry: ");
        if (scanf("%s", f_name) != 1)
            break;

        if (strcmp(f_name, "quit") == 0)
            break;

        printf("Enter last name: ");
        if (scanf("%s", l_name) != 1)
            break;

        addHuman(Mom, f_name, l_name, NULL, NULL);
    }

    Lst = Mom;

    while (1)
    {
        printf("First Name: %s  Last Name: %s\n", Lst->name, Lst->surname);

        if (Lst->next == NULL)
            break;
        Lst = Lst->next;
    }

    return 0;
}

字符串
以下是需要注意的几点。

  • 在某种程度上,需要创建初始结构条目,在这个原型中,这是通过“add_parent”函数完成的。
  • 在“main”函数中添加了一个简单的提示循环,以允许将其他人体结构添加到列表中。
  • 当添加结构时,在“findHuman”函数中使用字符串比较函数。
  • 如果允许的话,通过首先遍历当前链表,从初始(头)条目开始,直到“下一个”结构指针为“NULL”,来添加新的人体结构。

下面是一个简单的终端测试来说明初始结构列表的创建(有一个母亲和一个父亲)。当然,这可以用不同的方式来完成,但确实得到了一个初始列表。

craig@Vera:~/C_Programs/Console/Genealogy/bin/Release$ ./Genealogy 
Mom: Jane Smith
Dad: John Smith
Enter first name or "quit" to end entry: Bill
Enter last name: Smith
Enter first name or "quit" to end entry: Bill
Enter last name: Smith
Person exists already.
Enter first name or "quit" to end entry: Tom
Enter last name: Elliot
Enter first name or "quit" to end entry: quit
First Name: Jane  Last Name: Smith
First Name: John  Last Name: Smith
First Name: Bill  Last Name: Smith
First Name: Tom  Last Name: Elliot


最后,这个原型还需要进一步完善,但它应该为构建链表以及遍历链表提供思考的食物。另一个收获是可能深入研究一些“C”教程,因为它们与链表,字符串,指针和循环有关。

相关问题