0

I am working on a program that uses a binary search tree (as an exercise). My problem is that when I try to add a customer(in the middle of my code lines 65-69) I get an error that BS_node is undeclared, though I insert struct BST_node *root in this function.. Part of my code is below, just for the readers to read it easier, if requested I can upload the full code ! Thanks!

  #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;
}
3
  • Please indicate on which line the error is raised. But it seems that you use BST_node instead of struct BST_node. You could use a typedef directive to define BST_node to avoid repeating 'struct'. Commented Jan 21, 2017 at 12:44
  • I am referring to lines 65-69 of the code! In 65 i define the function and in 69 the error occurs wher i try to allocate memory.. Commented Jan 21, 2017 at 12:47
  • So the error is indeed due to the missing of struct. Commented Jan 21, 2017 at 13:30

1 Answer 1

1

On the line 69, you have to specify struct BST_node instead of BST_node:

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

As for the rest of the code: read the manual for gets, it's clearly a function one should not use, I'd advise replacing it with fgets. There's also a little closing bracket missing in your final switch (line 178).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.