我一直致力于在 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'
有谁知道是什么导致了如此多的错误?