0

I'm writing a program to take data to store in a BST. My problem is I don't know what's wrong with my MakeNewNode and Insert function. I tried both of them the printed data didn't come out as expected. Please help

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

    typedef struct example
    {
        char MSKH[13];
        char ten[30];
        char tong[12];
        int  thucpham;
        int dientu;
        int maymac;
    }KeyType;

    typedef struct node
    {
        KeyType key;
        struct node* left;
        struct node* right;
    }NodeType;

    typedef NodeType* TreeType;

    NodeType* MakeNewNode(KeyType a)
    {
        NodeType* NewNode;
        NewNode = (NodeType *)malloc(sizeof(NodeType));
        if (NewNode != NULL)
        {
            strcpy(a.MSKH,((NewNode)->key).MSKH);
            strcpy(a.ten,((NewNode)->key).ten);
            strcpy(a.tong,((NewNode)->key).tong);
            ((NewNode)->key).thucpham = a.thucpham;
            ((NewNode)->key).dientu = a.dientu;
            ((NewNode)->key).maymac = a.maymac;
            /*strcpy(a.thucpham,(NewNode->key).thucpham);
            strcpy(a.dientu,(NewNode->key).dientu);
            strcpy(a.maymac,(NewNode->key).maymac);*/
            (NewNode)->left = NULL;
            (NewNode)->right= NULL;
        }
        return NewNode;
    }

    void Insert(KeyType a, TreeType *Root)
    {
        if(*Root == NULL)
        {
            *Root = (NodeType *)malloc(sizeof(NodeType));
            strcpy(a.MSKH,((*Root)->key).MSKH);
            strcpy(a.ten,((*Root)->key).ten);
            strcpy(a.tong,((*Root)->key).tong);
            ((*Root)->key).thucpham = a.thucpham;
            ((*Root)->key).dientu = a.dientu;
            ((*Root)->key).maymac = a.maymac;
            /*strcpy(a.thucpham,(Root->key).thucpham);
            strcpy(a.dientu,(Root->key).dientu);
            strcpy(a.maymac,(Root->key).maymac);*/
        (*Root)->left = NULL;
            (*Root)->right= NULL;
        }
        else if (strcasecmp(a.MSKH,((*Root)->key).MSKH) < 0) Insert(a,&(*Root)->left);
        else if (strcasecmp(a.MSKH,((*Root)->key).MSKH) > 0) Insert(a,&(*Root)->right);
    }

    void Traverse(TreeType Root){
            if (Root != NULL)
            {
                Traverse(Root->left);
                printf("%s %s %s %d %d %d\n",(Root->key).MSKH,(Root->key).ten,(Root->key).tong,(Root->key).thucpham,(Root->key).dientu,(Root->key).maymac);
                Traverse(Root->right);
            }
    }

    int main(){
        FILE* fin, *fout;
        fin = fopen("F:\\text.txt","r");
        char line[256];
        TreeType root=NULL;
        KeyType hoadon;
        fscanf(fin,"%13s %30s %12s %d %d %d",&hoadon.MSKH,&hoadon.ten,&hoadon.tong,&hoadon.thucpham,&hoadon.dientu,&hoadon.maymac);
        printf("%s %s %s %d %d %d\n",hoadon.MSKH,hoadon.ten,hoadon.tong,hoadon.thucpham,hoadon.dientu,hoadon.maymac);
        root = MakeNewNode(hoadon);
        printf("%s",(root->key).MSKH);
        fclose(fin);
}

Example of data from txt file: CLA012032A1 NguyenNam 12000000 1 2 3

laylarenee
  • 3,276
  • 7
  • 32
  • 40
  • 1
    [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Nov 13 '15 at 15:07
  • what is the expected value? what is wrong exactly? – ForeverStudent Nov 13 '15 at 15:24
  • i got the example of the data above if i store data in a node correctly, the expected value comeout would be like the example, but when i print a node made from the file data, it got messed up – P. Nam Nguyen Nov 13 '15 at 15:32
  • 3
    Your strcpy calls are reversed. The source should be the second parameter – o_weisman Nov 13 '15 at 15:45
  • 1
    You don't need to add brackets when you use the member operators (-> and .), except in this case `(*Root)`. http://en.cppreference.com/w/c/language/operator_precedence Also, `main()` should be `main(void)`. Empty parameters doesn't mean no parameters. – Bobby Sacamano Nov 13 '15 at 18:08

0 Answers0