1

二分探索木の要素を入力し、その InOrder、PostOrder、および PreOrder トラバーサルを表示する C プログラムを作成しました。

#include<stdio.h>
#include<stdlib.h>
struct tnode
{
    int data;
    struct tnode *leftc;
    struct tnode *rightc;
};
int main()
{
    char ans='N';
    struct tnode *new_node,*root;
    //  struct tnode *get_node();
    root=NULL;
    do{
        // new_node=get_node();
        printf("\nEnter the Element");
        scanf("%d",&new_node->data);
        if(root==NULL)
            root=new_node;
        else
            insert(root,new_node);
        printf("\nDo you want to enter a new element?(y/n)");
        scanf("%c",&ans);
    }while(ans == 'y');
    printf("Inorder traversal:the elements in the tree are");
    inorder(root);
    printf("\nPreorder traversal:the elements in the tree are");
    preorder(root);
    printf("Postorder traversal:the elements in the tree are");
    postorder(root);
    return 0;
}
void insert(struct tnode ** tree,int num)
{
    struct tnode *temp = NULL;
    if(!(*tree))
    {
        temp=(struct tnode *)malloc(sizeof (struct tnode));
        temp->leftc=temp->rightc=NULL;
        temp->data=num;
        *tree=temp;
        return;
    }
    if(num < (*tree)->data)
    {
        insert(&(*tree)->leftc,num);
    }
    else if(num > (*tree)->data)
    {
        insert(&(*tree)->rightc,num);
    }
}
void preorder(struct tnode * s)
{
    if(s)
    {
        printf("%d\n",s->data);
        preorder(s->leftc);
        preorder(s->rightc);
    }
}
void inorder(struct tnode * s)
{
    if(s)
    {
        inorder(s->leftc);
        printf("%d\n",s->data);
        inorder(s->rightc);
    }
}
void postorder(struct tnode * s)
{
    if(s)
    {
        postorder(s->leftc);
        postorder(s->rightc);
        printf("%d\n",s->data);
    }
}

次の警告メッセージが表示されます。

warning: implicit declaration of functionS,
conflicting types OF FUNCTIONS,
new_node’ may be used uninitialized in this function

エラーを理解できません。これらを修正するのを手伝ってもらえますか?

4

2 に答える 2

3

Cで関数を使用するには、あなたの場合のようにmain関数の前にemを宣言する必要があります:

void insert(struct tnode ** tree,int num);
//all declarations of other functions here . 

//ところで、次のように変数名なしで em を宣言できます。

void insert(struct tnode ** , int );

また、 C で Binary Search Tree をグーグルで検索してみてください。あなたが探している答えを正確に示している多くのウェブサイトと、その周りのすべてを説明するチュートリアルを持っているウェブサイトがたくさんあります.

PS メイン関数の前に関数を宣言したくない場合は、メイン関数の上部に準備済みの関数を配置するだけでよく、メイン関数は最後に下部に配置する必要があります。

于 2016-07-14T14:12:29.803 に答える
2
  • 関数を使用する前に、使用する関数を宣言または定義する必要があります。
  • の使い方insert()が間違っています。
  • 不確定な自動保存期間を持つ初期化されていない変数の値を使用すると、未定義の動作が呼び出されます。この場合、構造体を使用して新しいノードのデータを読み取る必要はありません。
  • 予期しないものが に読み込まれる可能性がありますans。文字を読み取る前に空白文字%cをスキップするには、フォーマット指定子の前にスペースを追加します。scanf()
  • の標準シグネチャの 1 つを使用する必要がありますmain()。C では、int main()int main(void) は異なる意味を持ちます。
  • コードを適切にフォーマットする必要があります。
  • 彼らは、 Cの結果をキャストすべきではないと言っていますmalloc()

これを試して:

#include<stdio.h>
#include<stdlib.h>
struct tnode
{
    int data;
    struct tnode *leftc;
    struct tnode *rightc;
};
/* declare functions */
void insert(struct tnode ** tree,int num);
void preorder(struct tnode * s);
void inorder(struct tnode * s);
void postorder(struct tnode * s);
/* use one of the standard forms of main() */
int main(void)
{
    char ans='N';
    struct tnode *root;
    int new_node_data;
    //  struct tnode *get_node();
    root=NULL;
    do{
        // new_node=get_node();
        printf("\nEnter the Element");
        scanf("%d",&new_node_data); /* do not dereference indeterminate pointer */
        insert(&root,new_node_data); /* pass correct data */
        printf("\nDo you want to enter a new element?(y/n)");
        scanf(" %c",&ans); /* add a space before %c to have scanf() skip whitespace characters */
    }while(ans == 'y');
    printf("Inorder traversal:the elements in the tree are");
    inorder(root);
    printf("\nPreorder traversal:the elements in the tree are");
    preorder(root);
    printf("Postorder traversal:the elements in the tree are");
    postorder(root);
    return 0;
}
void insert(struct tnode ** tree,int num)
{
    struct tnode *temp = NULL;
    if(!(*tree))
    {
        temp=malloc(sizeof (struct tnode));
        temp->leftc=temp->rightc=NULL;
        temp->data=num;
        *tree=temp;
        return;
    }
    if(num < (*tree)->data)
    {
        insert(&(*tree)->leftc,num);
    }
    else if(num > (*tree)->data)
    {
        insert(&(*tree)->rightc,num);
    }
}
void preorder(struct tnode * s)
{
    if(s)
    {
        printf("%d\n",s->data);
        preorder(s->leftc);
        preorder(s->rightc);
    }
}
void inorder(struct tnode * s)
{
    if(s)
    {
        inorder(s->leftc);
        printf("%d\n",s->data);
        inorder(s->rightc);
    }
}
void postorder(struct tnode * s)
{
    if(s)
    {
        postorder(s->leftc);
        postorder(s->rightc);
        printf("%d\n",s->data);
    }
}
于 2016-07-14T14:14:20.537 に答える