-3

c (バイナリ ツリーを使用) を使用してプログラムを作成する必要があります。このプログラムは、学生の名前と ID を保存し、名前の挿入、削除、検索、表示を前順、順番、後順で実行できるようにするために必要です。動作しません。挿入に問題があり、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

1 に答える 1

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 配列で、data は int です。あなたが望んでいたと思いますtemp->name = word

これは、多くの警告なしにコンパイルされないためです。コンパイルエラーや警告を引き起こす他の場所もあると思うので、最初にコンパイルするようにしてください。

gcc を使用している場合は、フラグを追加して、-Wall -Wextraバグの修正に役立つ可能性がある多くの警告を表示します。


insert を呼び出す場所を見てみましょう。

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] は char を返し、それを配列を探す関数に渡します。あなたはそれをinsert(&root,a,b)
  3. コンパイラの警告をオンにします。
于 2013-05-18T00:05:02.683 に答える