2013-05-17 22 views
-3

我需要用c(使用二叉樹)創建一個程序這個程序是保存學生姓名和ID所必需的,並且允許你在預訂中插入,刪除,搜索和顯示名字,併發布命令和我寫的代碼,但它沒有工作有一個插入問題,它只需要身份證,它不採取的名稱,我想知道這個代碼是什麼問題在二叉樹中插入一個字符串

 #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 


struct bin_tree 
{ 
int data; 
char name[100]; 
struct bin_tree * right, * left; 
}; 
typedef struct bin_tree node; 

    void insert(node ** tree, int val, char word[100]) 
{ 
node *temp = NULL; 
if(!(*tree)) 
{ 
    temp = (node *)malloc(sizeof(node)); 
    temp->left = temp->right = NULL; 
    temp->data = val; 
    temp->data = word; 
    *tree = temp; 
    return; 
} 

if(val < (*tree)->data) 
{ 
    insert(&(*tree)->left, val,word); 
} 
else if(val > (*tree)->data) 
{ 
    insert(&(*tree)->right, val,word); 
} 

} 


    struct tree *delet(struct bin_tree *ptr,int x) 
{ 
struct bin_tree *p1,*p2; 
if(!ptr) 
{ 
    printf("\n student not found "); 
    return(ptr); 
} 
else 
{ 
    if(ptr->data < x) 
    { 
     ptr->right = delet(ptr->right,x); 

    } 
    else if (ptr->data >x) 
    { 
     ptr->left=delet(ptr->left,x); 
     return ptr; 
    } 
    else 
    { 
     if(ptr->data == x) 
     { 
      if(ptr->left == ptr->right) 
      { 
       free(ptr); 
       return(NULL); 
      } 
      else if(ptr->left==NULL) 
      { 
       p1=ptr->right; 
       free(ptr); 
       return p1; 
      } 
      else if(ptr->right==NULL) 
      { 
       p1=ptr->left; 
       free(ptr); 
       return p1; 
      } 
      else 
      { 
       p1=ptr->right; 
       p2=ptr->right; 
       while(p1->left != NULL) 
        p1=p1->left; 
       p1->left=ptr->left; 
       free(ptr); 
       return p2; 
      } 
     } 
    } 
} 
return(ptr); 
} 

node* search(node ** tree, int val) 
{ 
if(!(*tree)) 
{ 
    return NULL; 
} 

if(val < (*tree)->data) 
{ 
    search(&((*tree)->left), val); 
} 
else if(val > (*tree)->data) 
{ 
    search(&((*tree)->right), val); 
} 
else if(val == (*tree)->data) 
{ 
    return *tree; 
} 
} 
void print_preorder(node * tree) 
{ 
    if (tree) 
{ 
    printf("%d\n%s\n",tree->data,tree->name); 
    print_preorder(tree->left); 
    print_preorder(tree->right); 
} 

} 

    void print_inorder(node * tree) 
{ 
if (tree) 
{ 
    print_inorder(tree->left); 
    printf("%d\n%s\n",tree->data,tree->name); 
    print_inorder(tree->right); 
} 
} 

void print_postorder(node * tree) 
{ 
if (tree) 
{ 
    print_postorder(tree->left); 
    print_postorder(tree->right); 
    printf("%d\n%s\n",tree->data,tree->name); 
} 
} 



void main() 
{ 
node *root; 
node *tmp; 
int a,item_no,z,i=0; 
char x,b[100],c; 

root=NULL; 
while(x!='5') 
{ 
    printf("\nmenu\n----\n1. insert\n2. delete\n3. search\n4. display\n5. end program  \n\n"); 

    printf("\nEnter the choice:"); 
    scanf("%s",&x); 
    switch(x) 
    { 
    case '1': 
    { 
     printf("\nEnter the id: "); 
     scanf("%d",&a); 
     printf("\nenter the name: "); 
     while ((c = getchar()) != '\n' && i < 100) 
     { 
      b[ i++ ] = c; 
     } 
     b[ i ] = '\0'; 
     insert(&root,a,b[100]); 
     break; 
    } 
    case '2': 
    { 
     printf("\n Enter the student id to be deleted : "); 
     scanf(" %d",&item_no); 
     root=delet(root,item_no); 
     break; 
    } 
    case '3': 
    { 
     printf("\nEnter student id: "); 
     scanf("%d",&z); 
     tmp = search(&root, z); 
     if (tmp) 
     { 
      printf("\nstudent id=%d\nstudent name: %s\n", tmp->data,tmp->name); 
     } 
     else 
     { 
      printf("\nData Not found.\n"); 
     } 
    } 
    case '4': 
    { 
     printf("Pre Order Display\n"); 
     print_preorder(root); 

     printf("In Order Display\n"); 
     print_inorder(root); 

     printf("Post Order Display\n"); 
     print_postorder(root); 
    } 
    case '\t': 
    case '\n': 
    case ' ': 
    default :{ 
    printf("\nwrong entery!\n"); 
    break; 
    } 

    } 


} 

return 0; 
} 
} 
+4

那麼先關閉。什麼不按照你期望的方式工作? (並可能爲您的問題添加更好的標題) – FDinoff

+0

感謝您的建議:D – user2395710

回答

0

那麼讓我們來看看你的插入功能。

void insert(node ** tree, int val, char word[100]) 
{ 
    node *temp = NULL; 
    if(!(*tree)) 
    { 
     temp = (node *)malloc(sizeof(node)); 
     temp->left = temp->right = NULL; 
     temp->data = val; 
     temp->data = word; 
     *tree = temp; 
     return; 
    } 
... 

我認爲問題是,temp->data = word。我的編譯器(gcc)生成一個警告assignment makes integer from pointer without a cast。 word是一個char數組,數據是一個int。我假設你想要temp->name = word

因爲這不編譯沒有很多的警告。我認爲你應該嘗試首先編譯它,因爲我認爲還有其他地方會導致編譯錯誤或警告。

如果您使用的是gcc,請添加標記-Wall -Wextra以顯示大量可能有助於修復錯誤的警告。


我們如何看待我們所謂的插入。

case '1': 
{ 
    printf("\nEnter the id: "); 
    scanf("%d",&a); 
    printf("\nenter the name: "); 
    while ((c = getchar()) != '\n' && i < 100) 
    { 
     b[ i++ ] = c; 
    } 
    b[ i ] = '\0'; 
    insert(&root,a,b[100]); 
    break; 
} 

這裏您傳遞b [100]作爲單詞。

  1. B定義爲char b[100]因此調用B [100]是要訪問的陣列外部未定義的行爲。
  2. b [100]返回一個字符,並將其傳遞給正在查找數組的函數。你應該把它叫做insert(&root,a,b)
  3. 打開編譯器警告。
+0

謝謝你真的幫我糾正錯誤,並從中學習...但程序仍然無法正常工作,我將temp-> data temp-> name [100]和b [100] to b,但仍然存在錯誤 – user2395710

+0

@ user2395710'temp-> name [100] = ...'將100個位置分配給數組中的第100個位置。它應該是'temp-> name'。我建議爲c寫一本好書,因爲這是非常基本的東西。 – FDinoff

+0

它給我錯誤,當我寫te​​mp->名稱,當我寫te​​mp->名稱[100] – user2395710