简体   繁体   English

二进制搜索树问题从C中的txt导入名称

[英]Binary search tree Problem Importing names from txt in C

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. 我有一个作业,要求我从文本文档中插入100个学生姓名和ID,格式如(姓氏ID)广告,然后将其放入两个二进制搜索树中。 The main BST will contain surnames and a pointer to the other BST which will contain names and IDs. 主BST将包含姓氏和指向另一个BST的指针,该BST将包含名称和ID。 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. 不管怎么说,还是要谢谢你。

Actually, you've got the hard part. 实际上,您有困难的部分。 The problem is strcpy you just want 问题是您只想要strcpy

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. st->name一句,我对您在loadData函数中释放st->name感到不舒服。 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. 希望st->nameNULL ,但是一种更可取的方法是拥有一个单独的destroy函数,以释放整个树。 Then you can pair the loadData and destroyData function. 然后,您可以将loadDatadestroyData函数配对。 It is always best to have allocates and frees as pairs this way. 始终最好以这种方式将分配和释放成对。 It makes it unlikely you will leak memory, double free, etc. 它使您不太可能泄漏内存,双倍可用空间等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM