我在c中编写了一个二元搜索树,我一直在傻傻的"错误:期待')'之前' *'令牌"我的函数在其声明和头文件中开头的消息..
主档
#include <stdlib.h>
#include <stdio.h>
#include "func.h"
#define ARRAYSIZE 12
int main(int argc, char *argv[3]) {
typedef struct node {
int value;
struct node * left;
struct node * right;
} * node;
node* root = NULL;
int nodes[ARRAYSIZE] = {3,6,2,1,7,8,3,5,7,2,9,4};
int i;
for(i = 0; i < ARRAYSIZE; i++) {
root = insert(root, nodes[i]);
}
if (strcmp(argv[1], "q") == 0) quit();
if (strcmp(argv[1], "i") == 0) insert(argv[2]);
if (strcmp(argv[1], "d") == 0) delete(argv[2]);
if (strcmp(argv[1], "s") == 0) search(argv[2]);
if (strcmp(argv[1], "e") == 0) empty();
return 0;
}
函数文件
#include "func.h"
node * createNode(int value) {
node * new_node = (node*)malloc(sizeof(node));
if(new_node == NULL) {
exit(1);
}
new_node->value = value;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
void insert(node * root, int n){
if(root == NULL) {
root = createNode(n);
}
else {
int left = 0;
node * current = root;
node * previous = NULL;
while(current != NULL){
previous = current;
if((n == current->n) < 0) {
left = 1;
current = current->left;
}
else if((n == current->n) > 0) {
left = 0;
current = current->right;
}
}
if(left)
previous->left = createNode(n);
else
previous->right = createNode(n);
}
}
void delete(node * root, int n){
node *current, *parent, *successor, *presuccessor, q;
if (root->left == NULL) {
printf("\ntree is empty! (deleting)");
}
parent = root;
current = root->left;
while (current !=NULL && n != current->value) {
parent = current;
current = (n next) ? current->left : current->right;
/* maybe add the } here ;) */
if (current == NULL) {
printf("\n %d is missing \n", n);
}
// Item found, now delete it
if (current->left == NULL)
q = current->right;
else if(current->right == NULL)
q = cur->left;
else {
// Obtain the inorder successor and its parent
presuccessor = current;
current = current->left;
while (successor->left != NULL) {
presuccessor = successor;
successor = successor->left;
}
if (current == presuccessor) { /*situation 1*/
successor->left = current->right;
}
else { /*situation 2*/
successor->left = current->left;
presuccessor->left = successor->right;
successor->right = current->right;
}
q = successor;
}
if (parent->left == current)
parent->left = q;
else
parent->right = q;
freeNode(current);
}
void search(node * root, int n){
if (root == NULL){
return NULL;
}
node * current = root;
while (current != null){
if (current->value > n) {
current = current->left;
}
else if (current->value < n){
current = current->right;
}
else
printf("%d is present", n)
}
printf("n is missing");
}
void empty(node * root){
if(root != NULL) {
empty(root->left);
empty(root->right);
free(root);
}
}
/* Print the value at each node with a single space character */
/* Else if tree is empty print "tree is empty" */
/* Do this for the following functions */
/*void inTraversal(node * root){
if node == null
return;
inTraversal(node.left)
visit(node)
inorder(node.right)
}
void preTraversal(){
preorder(node)
if node == null then return
visit(node)
preorder(node.left)
preorder(node.right)
}
void postTraversal(){
postorder(node)
if node == null then return
postorder(node.left)
postorder(node.right)
visit(node)
}*/
int quit() {
exit(1);
return 0;
}
标题
#ifndef FUNC_H
#define FUNC_H
node * createNode(int value);
void insert(node * root, int n);
void search(node * root, int n);
void empty(node * root);
void delete(node * root, int n);
int quit();
#endif
答案 0 :(得分:5)
可能是:
node * createNode(int value);
那时 node
是未知的。
将node
的定义移到您的头文件中。