实现二叉搜索树时的语法错误(C)

时间:2013-04-28 16:10:28

标签: c function struct syntax-error binary-tree

我一直在用C语言实现一个二进制搜索树,用于编程课程中的家庭作业。在Visual Studio 2010中编写了大量代码和编译之后,我遇到了很多错误。

我已经在网上找到了类似错误的人,但他们都没有足够接近我找出问题所在。

以下是代码:

/*This code will implement a binary search tree based on 20
user-inputed integers. It will then implement a search,
insert, delete, and traverse function. */

#include <stdio.h>
#include <stdlib.h>
#define ARRSIZE 20

/*------ TYPE DEFINITIONS ------*/

typedef struct node
{
    int data;
    node* left;
    node* right;
};

typedef struct tree
{
    int count;
    node* root;
};

/*------ FUNCTION DECLARTATIONS ------*/

void InitializeTree (tree* ptree);
int insert (int data, tree* ptree);
void place (node* root, node* new_node);

/*------ MAIN FUNCTION ------*/

int main (void)
{
    int i, check, arr[ARRSIZE];
    tree BST;

    InitializeTree(&BST);

    printf("Enter 20 integers for a list.\n");

    for(i=0; i<ARRSIZE; i++)
    {
        printf("Integer %d:\n", i+1);
        scanf("%d", &arr[i]);
        check = insert(arr[i], &BST);
        if(check == 0)
        {
            printf("Error in creating node.\n");
            exit(1);
        }
    }

    return 0;
}

/*
*
* ADDITIONAL FUNCTIONS
*
*/

/*------ CREATION FUNCTIONS ------*/

void InitializeTree (tree* ptree)
{
    ptree = (tree*) malloc(sizeof(tree));
    ptree->count = 0;
}

int insert (int data, tree* ptree)
{
    node* new_node;

    new_node = (node*) malloc(sizeof(node));
    if(new_node == NULL)
    {
        printf("Couldn't create a node.\n");
        return 0;
    }
        new_node->data = data;

    ptree->count++;

    if(ptree->root == NULL)
    {
        ptree->root = new_node;
    }

    else
    {
        place(ptree->root, new_node);
    }
    return 1;

}

void place (node* root, node* new_node)
{
    if(new_node->data < root->data)
    {
        if(root->left == NULL)
        {
            root->left = new_node;
        }
        else
        {
            place(root->left, new_node);
        }
    }
    else
    {
        if(root->right == NULL)
        {
            root->right = new_node;
        }
        else
        {
        place(root->right, new_node;
        }
    }
}

错误如下所示:

1>bin-tree.c(16): error C2061: syntax error : identifier 'node'
1>bin-tree.c(17): error C2143: syntax error : missing '{' before '*'
1>bin-tree.c(18): error C2059: syntax error : '}'
1>bin-tree.c(23): error C2061: syntax error : identifier 'node'
1>bin-tree.c(24): error C2059: syntax error : '}'
1>bin-tree.c(28): error C2143: syntax error : missing ')' before '*'
1>bin-tree.c(28): error C2143: syntax error : missing '{' before '*'
1>bin-tree.c(28): error C2059: syntax error : ')'
1>bin-tree.c(29): error C2143: syntax error : missing ')' before '*'
1>bin-tree.c(29): error C2081: 'tree' : name in formal parameter list illegal
1>bin-tree.c(29): error C2143: syntax error : missing '{' before '*'
1>bin-tree.c(29): error C2059: syntax error : ')'
1>bin-tree.c(30): error C2143: syntax error : missing ')' before '*'
1>bin-tree.c(30): error C2143: syntax error : missing '{' before '*'
1>bin-tree.c(30): error C2143: syntax error : missing ';' before '*'
1>bin-tree.c(30): error C2059: syntax error : ')'
1>bin-tree.c(37): error C2065: 'tree' : undeclared identifier
1>bin-tree.c(37): error C2146: syntax error : missing ';' before identifier 'BST'
1>bin-tree.c(37): error C2065: 'BST' : undeclared identifier
1>bin-tree.c(39): warning C4013: 'InitializeTree' undefined; assuming extern returning int
1>bin-tree.c(39): error C2065: 'BST' : undeclared identifier
1>bin-tree.c(47): warning C4013: 'insert' undefined; assuming extern returning int
1>bin-tree.c(47): error C2065: 'BST' : undeclared identifier
1>bin-tree.c(66): error C2143: syntax error : missing ')' before '*'
1>bin-tree.c(66): error C2143: syntax error : missing '{' before '*'
1>bin-tree.c(66): error C2059: syntax error : ')'
1>bin-tree.c(67): error C2054: expected '(' to follow 'ptree'
1>bin-tree.c(72): error C2143: syntax error : missing ')' before '*'
1>bin-tree.c(72): error C2081: 'tree' : name in formal parameter list illegal
1>bin-tree.c(72): error C2143: syntax error : missing '{' before '*'
1>bin-tree.c(72): error C2059: syntax error : ')'
1>bin-tree.c(73): error C2054: expected '(' to follow 'ptree'
1>bin-tree.c(99): error C2143: syntax error : missing ')' before '*'
1>bin-tree.c(99): error C2143: syntax error : missing '{' before '*'
1>bin-tree.c(99): error C2143: syntax error : missing ';' before '*'
1>bin-tree.c(99): error C2059: syntax error : ')'
1>bin-tree.c(100): error C2054: expected '(' to follow 'new_node'

有谁知道可能导致如此众多错误的原因?

1 个答案:

答案 0 :(得分:4)

更改您的typedef:

typedef struct node
{
    int data;
    node* left;
    node* right;
};

typedef struct tree
{
    int count;
    node* root;
};

为:

typedef struct node
{
    int data;
    struct node* left;   // <<<
    struct node* right;  // <<<
} node;                  // <<<

typedef struct tree
{
    int count;
    node* root;
} tree;                  // <<<

同样如@Binayaka所述,你有一个缺失的括号 - 更改

    place(root->right, new_node;

为:

    place(root->right, new_node);