二叉搜索树在C中从txt导入名称问题

发布于 2024-11-08 15:35:05 字数 1758 浏览 0 评论 0原文

我有一个作业,要求我从文本文档中插入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝天白云 2024-11-15 15:35:05

事实上,你已经遇到了困难的部分。问题是 strcpy 你只想

strcpy(node->surname, sname)

将传入的姓氏复制到节点结构中。

顺便说一句,我对您在 loadData 函数中释放 st->name 感到有点不舒服。第一次调用该函数时会发生什么?希望 st->nameNULL,但更好的方法是使用一个单独的销毁函数来释放整个树。然后您可以将 loadDatadestroyData 函数配对。最好以这种方式成对分配和释放。它使得你不太可能泄漏内存、双重释放等。

Actually, you've got the hard part. The problem is strcpy you just want

strcpy(node->surname, sname)

to copy the surname passed in into the node structure.

As an aside, I'm a little uncomfortable with your freeing st->name in your loadData function. What happens the first time you call the function? Hopefully st->name is NULL, but a preferable way would be to have a separate destroy function that frees up an entire tree. Then you can pair the loadData and destroyData 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文