2014-03-30 50 views
-1

我正在編寫一個二叉搜索樹,並且在'*'token'消息開始之前,我一直得到愚蠢的「error:expected')'我在他們的聲明,並在頭文件功能..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 
+5

如果錯誤沒有告訴它發生的位置的行號,那麼錯誤就更加愚蠢。 – mah

+1

而問題往往會變成_off-topic_,因爲__it缺乏足夠的信息來診斷問題___。 – devnull

+0

有趣的是:'node a'與'struct node a'不一樣!我希望這不是故意的? – Deduplicator

回答

5

很可能是:

node * createNode(int value); 

node是在這一點不得而知。

node的定義移動到您的頭文件中。

+0

不止「可能」......他的「主文件」有一個#include的func.h,它需要節點類型,但類型僅在此後在該主文件中定義。此外,他的「函數文件」根本無法訪問類型定義。 – mah

+0

這造成了大量生產的錯誤「請求成員'左'/'右'/'價值'的東西不是一個結構或聯盟」 – user3362954

+0

@ user3362954你不能繼續傾銷你的編譯器錯誤在這裏。你會得到這個錯誤,因爲節點需要在struct節點之前被聲明。 – thumbmunkeys