简体   繁体   中英

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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