我正在编写一个程序来将数据存储在BST中。 我的问题是我不知道我的MakeNewNode和Insert函数有什么问题。 我试过他们两个打印的数据没有按预期出来。请帮忙
#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);
}
来自txt文件的数据示例:CLA012032A1 NguyenNam 12000000 1 2 3