指针和二进制搜索树

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

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

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

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

至于代码的其余部分:阅读手册gets ,它显然是一个不应该使用的function,我建议用fgets替换它。 最后一个switch还缺少一个关闭括号(第178行)。