简体   繁体   English

二进制搜索树和结构错误

[英]binary search tree and struct errors

This program should create a binary search tree, insert numbers 1-366 into the tree and then should prompt the user to enter a number to see if the number is in the binary search tree. 该程序应创建一个二进制搜索树,在树中插入数字1-366,然后提示用户输入数字以查看该数字是否在二进制搜索树中。 It worked when it was all in one C file but I now need to make it modular and have main in a separate file but it now gives me errors that I don't understand. 当它全部放在一个C文件中时,它可以工作,但是我现在需要使其模块化并在一个单独的文件中拥有main,但现在却给了我我不明白的错误。

I've got 3 files: 我有3个文件:

map.h: map.h:

#include <stdbool.h>

typedef struct tree Tree;
typedef struct node Node;

//creates a new tree
Tree *new_tree();

//create a new node
Node* NewNode(int data);

//insert in to the binary tree
Node* insert(Node* node, int data);

//search for nodes to see if they exist
bool NodeSearch(Node* node,int data);

map.c: map.c:

//Binary search tree implementation
#include <stdio.h>
#include <stdlib.h>
#include "map.h"

//node structure
struct node {
    int data;
    struct node* left;
    struct node* right;
} ;

//tree wrapper structure
struct tree {
    struct node *root;
} ;

//create a new tree
Tree *new_tree() {
    Tree *t = malloc(sizeof(Tree));
    t->root = NULL;
    return t;
}

//create a new node
Node* NewNode(int data) {
  Node* node = malloc(sizeof *node);    // "new" is like "malloc"
  node->data = data;
  node->left = NULL;
  node->right = NULL;

  return(node);
}

//insert in to the binary tree
Node* insert(Node* node, int data) {
  // 1. If the tree is empty, return a new, single node
  if (node == NULL) {
    return(NewNode(data));
  }
  else {
    // 2. Otherwise, recur down the tree
    if (data <= node->data) node->left = insert(node->left, data);
    else node->right = insert(node->right, data);

    return(node); // return the (unchanged) node pointer
  }
}
//search for nodes to see if they exist
bool NodeSearch(Node* node,int data) {
    if(node==NULL) return false;
    else if(node->data == data) return true;
    else if(data  <= node ->data) return NodeSearch(node->left, data);
    else return NodeSearch(node->right, data);
}

mainphone.c: mainphone.c:

# include "map.h"
# include <stdio.h>
# include <stdlib.h>

int main(){
    struct node* root = NULL;

    for (int i = 1; i < 367; i++) {
        root = insert(root,i);
        Node *n = NewNode (i);
        insert (n, n->data);}

    int number;
    printf("Enter Number\n");
    scanf("%d",&number);
    if(NodeSearch(root, number) == true) printf("Found\n");
    else printf("Not found\n");


}

the errors it gives me are: 它给我的错误是:

mainphone.c:11:15: error: incomplete definition of type 'struct node'
            insert (n, n->data);}
                       ~^

./map.h:4:16: note: forward declaration of 'struct node'
typedef struct node Node;
           ^

mainphone.c:16:5: error: implicit declaration of function 'NodeSearch' is
  invalid in C99 [-Werror,-Wimplicit-function-declaration]
    if(NodeSearch(root, number) == true) printf("Found\n");

First error is because while main.c has the declaration of a struct node, the actual structure definition is in map.c, and mainphone.c does not know it. 第一个错误是因为main.c具有结构节点的声明,而实际的结构定义在map.c中,而mainphone.c不知道。

Now this is usually a good thing, since it forces us to encapsulate. 现在这通常是一件好事,因为它迫使我们封装。 If you want to keep your structures in map.c, you'll need to create functions to access their data from outside. 如果要将结构保留在map.c中,则需要创建函数以从外部访问其数据。

The second error is most likely a compilation one. 第二个错误很可能是编译错误。 Be sure to either link a previously compiled map.o to mainphone.c: 确保将先前编译的map.o链接到mainphone.c:

gcc (FLAGS_HERE) map.c -c
gcc (FLAGS HERE) map.o mainphone.c -o (YOUR_EXECUTABLE_NAME)

or use the automagical *.c syntax. 或使用自动* .c语法。

gcc (FLAGS_HERE) *.c -o (YOUR_EXECUTABLE_NAME)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM