二叉搜索树在C中从txt导入名称问题
我有一个作业,要求我从文本文档中插入 100 个学生姓名和 ID,格式如(姓氏 ID)广告,然后将其放入两个二叉搜索树中。主 BST 将包含姓氏和指向另一个包含姓名和 ID 的 BST 的指针。这是我第一次尝试使用指针(*,->,&),所以我迷失了。我设法使用以下函数导入文本
void loadData(char fname[], Students *st){
struct Students *new;
root=NULL;
int i;
FILE *fp;
fp=fopen(fname,"r");
if (fp == NULL) printf("File does not exist\n");
fscanf(fp, "%d", &(st->size)); //reads the number of students
free(st->name);
st->name=(Name*) malloc(st->size*(sizeof(Name)));
for (i=0; i<st->size; i++){
fscanf(fp, "%s",&st);
insert(root,st.surname);/////////I think here is the problem
//fscanf(fp, "%s", &st->name[i].firstname);
// fscanf(fp, "%d", &st->name[i].id);
}
fclose(fp);
}
现在我正在尝试创建插入函数,这对我来说非常困难,因为我无法理解她应该采用的参数
STU *insert(STU *node, char *sname)///What should i use here to save take the Surname??
{
if(node==NULL){
node=(NODE *) malloc(sizeof(STU));
strcpy(node->surname);
node->left=NULL;
node->right=NULL;
}
else{
if(strcmp(*sname, node->surname)<0)
insert(node->left, *sname);
else if(strcmp(*sname, node->surname)>0)
insert(node->right, *sname);
}
return node;
}
这是结构定义:
typedef struct Name{
char firstname[20];
int id;
struct Students *nameleft;
struct Students *nameright;
} Name;
typedef struct Students{
char surname[20];
Name *name;
int size;
struct Students *left;
struct Students *right;
} Students;
typedef struct Students STU;
struct Students *insert(char num);
struct Students *root=NULL;
任何人都可以帮我纠正插入吗函数,因为我无法理解必须使用哪些参数来保存姓氏,其余的我将自己完成。我认为我的问题是插入功能。 不管怎样,谢谢。
I have a homework which ask fro me to insert from a text document 100 students names and IDs formatted like(Surname Name ID) ad then put the in two binary search trees. The main BST will contain surnames and a pointer to the other BST which will contain names and IDs. This is the first time that i'm trynig to use pointers(*,->,&) so i'm LOST. I managed to import the text with the following function
void loadData(char fname[], Students *st){
struct Students *new;
root=NULL;
int i;
FILE *fp;
fp=fopen(fname,"r");
if (fp == NULL) printf("File does not exist\n");
fscanf(fp, "%d", &(st->size)); //reads the number of students
free(st->name);
st->name=(Name*) malloc(st->size*(sizeof(Name)));
for (i=0; i<st->size; i++){
fscanf(fp, "%s",&st);
insert(root,st.surname);/////////I think here is the problem
//fscanf(fp, "%s", &st->name[i].firstname);
// fscanf(fp, "%d", &st->name[i].id);
}
fclose(fp);
}
And now I'm trying to create the insert function which is very difficult for me because i cannot understand the arguments that she should take
STU *insert(STU *node, char *sname)///What should i use here to save take the Surname??
{
if(node==NULL){
node=(NODE *) malloc(sizeof(STU));
strcpy(node->surname);
node->left=NULL;
node->right=NULL;
}
else{
if(strcmp(*sname, node->surname)<0)
insert(node->left, *sname);
else if(strcmp(*sname, node->surname)>0)
insert(node->right, *sname);
}
return node;
}
Here is the structure definition:
typedef struct Name{
char firstname[20];
int id;
struct Students *nameleft;
struct Students *nameright;
} Name;
typedef struct Students{
char surname[20];
Name *name;
int size;
struct Students *left;
struct Students *right;
} Students;
typedef struct Students STU;
struct Students *insert(char num);
struct Students *root=NULL;
Can anyone help me correct the insert function because i cannot understand which arguments i must use to save the surname and i will do the rest myself. I think that my problem is the insert function.
Thanks anyway.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实上,你已经遇到了困难的部分。问题是
strcpy
你只想将传入的姓氏复制到节点结构中。
顺便说一句,我对您在
loadData
函数中释放st->name
感到有点不舒服。第一次调用该函数时会发生什么?希望st->name
为NULL
,但更好的方法是使用一个单独的销毁函数来释放整个树。然后您可以将loadData
和destroyData
函数配对。最好以这种方式成对分配和释放。它使得你不太可能泄漏内存、双重释放等。Actually, you've got the hard part. The problem is
strcpy
you just wantto copy the surname passed in into the node structure.
As an aside, I'm a little uncomfortable with your freeing
st->name
in yourloadData
function. What happens the first time you call the function? Hopefullyst->name
isNULL
, but a preferable way would be to have a separate destroy function that frees up an entire tree. Then you can pair theloadData
anddestroyData
function. It is always best to have allocates and frees as pairs this way. It makes it unlikely you will leak memory, double free, etc.