【问题标题】:Pointers and binary search tree指针和二叉搜索树
【发布时间】:2017-01-21 12:33:32
【问题描述】:

我正在开发一个使用二叉搜索树的程序(作为练习)。 我的问题是,当我尝试添加客户(在我的代码行 65-69 中间)时,我收到一个错误,即 BS_node 未声明,尽管我在此函数中插入了 struct BST_node *root。 . 我的部分代码如下,仅供读者阅读,如果需要我可以上传完整代码!谢谢!

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

#define MAX_STRING 50

void flush();

struct customer {
    char *name;
    char *address;
    char *email;
};

    struct double_linked_list {
    struct customer *data;
    struct double_linked_list *previous;
    struct double_linked_list *next;
};

    struct double_linked_list *customers_head=0;

    struct BST_node {
    struct double_linked_list *data;
    struct BST_node *left;
    struct BST_node *right;
};

    struct BST_node *BST_email_root = 0;

    struct BST_node *BST_find_customer(struct BST_node *root, char *email) {
    if (root==NULL)
        return NULL;
    if (strcmp(email,root->data->data->email)==0)
        return root;
    else
    {
    if (strcmp(email,root->data->data->email)==-1)
        return BST_find_customer(root->left,email);
    else
        return BST_find_customer(root->right,email);
    }
}

    void find_customer() {
    char email[MAX_STRING];
    struct double_linked_list *l;
    struct BST_node *b;
    printf("Give the email of the customer (up to %d characters) : ", MAX_STRING-1);
    gets(email);

    b = BST_find_customer(BST_email_root, email);
    if (b==0)
        printf("There is no customer with this email.\n");
    else
    {
        l = b->data;
        printf("Customer found! \n");
        printf("Name    : %s\n", l->data->name);
        printf("Address : %s\n", l->data->address);
        printf("Email   : %s\n", l->data->email);
    }
}

struct BST_node *new_BST_node(struct BST_node *root, struct double_linked_list *l)
{
if (root==NULL);
    {
    root = (BST_node *) malloc (sizeof(BST_node ));
    if (root==NULL)
        {
        printf("Out of Memory!");
        exit(1);
        }
    root->data=l;
    root->left=NULL;
    root->right=NULL;
    }

if (strcmp(l->data->email,root->data->data->email)==-1)
            root->left =new_BST_node(root->left,l);
else root->right =new_BST_node(root->right,l);

return root;
};

struct double_linked_list *new_customer()
{
    char name[MAX_STRING], address[MAX_STRING], email[MAX_STRING];
    struct BST_node *b;
    struct double_linked_list *l;
    struct customer *c;

    printf("\nADDING NEW CUSTOMER\n=\n\n");
    printf("Give name (up to %d characters): ", MAX_STRING-1);
    gets(name);

    printf("Give address (up to %d characters): ", MAX_STRING - 1);
    gets(address);

    printf("Give email (up to %d characters): ", MAX_STRING - 1);
    gets(email);


    b = BST_find_customer(BST_email_root, email);
    if (b)
    {
        printf("Duplicate email. Customer aborted.\n");
        return 0;
    }

    c = (struct customer *) malloc(sizeof(struct customer));
    if (c == 0)
    {
        printf("Not enough memory.\n");
        return 0;
    }

    c->name = strdup(name); // check for memory allocation problem
    if (c->name == 0) return 0;
    c->address = strdup(address);   // check for memory allocation problem
    if (c->address == 0) return 0;
    c->email = strdup(email);   // check for memory allocation problem
    if (c->email == 0) return 0;

    l = (struct double_linked_list*) malloc(sizeof(struct double_linked_list));
    if (l == 0)
    {
        printf("Not enough memory.\n");
        free(c->name);
        free(c->address);
        free(c->email);
        free(c);
        return 0;
    }

    l->data = c;
    l->previous = 0;
    l->next = customers_head;

    if (customers_head)
        customers_head->previous = l;

    customers_head = l;

    BST_email_root = new_BST_node(BST_email_root, l);

    return l;
}

void displaymenu() {
    printf("\n\n");
    printf("1. New customer\n");
    printf("2. Find customer using email\n");
    printf("0. Exit\n\n");
    printf("Give a choice (0-6) : ");
}

    void flush()
{
    char ch;
    while ((ch = getchar()) != '\n' && ch != EOF);
}


int main() {
int choice;
do {
    displaymenu();
    scanf("%d", &choice);
    flush();
    switch (choice) {
    case 1:
        new_customer();
        break;
    case 2:
        find_customer();
        break;
} while (choice != 0);

return 0;
}

【问题讨论】:

  • 请指出错误出现在哪一行。但您似乎使用BST_node 而不是struct BST_node。您可以使用 typedef 指令来定义 BST_node 以避免重复 'struct'。
  • 我指的是代码的第 65-69 行!在 65 中我定义了函数,在 69 中我尝试分配内存时发生错误..
  • 所以错误确实是由于缺少struct

标签: c function pointers binary-search-tree


【解决方案1】:

在第 69 行,您必须指定 struct BST_node 而不是 BST_node

root = (struct BST_node *) malloc (sizeof(struct BST_node ));

至于其余代码:阅读gets 的手册,这显然是一个不应该使用的函数,我建议将其替换为fgets。最后的 switch(第 178 行)中还缺少一个小括号。

【讨论】:

    猜你喜欢
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多