Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5929591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:26:30+00:00 2026-05-22T14:26:30+00:00

I have a homework which ask fro me to insert from a text document

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:26:30+00:00Added an answer on May 22, 2026 at 2:26 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a prolog homework, which should work like this: singles([1,2,3,2,2,4,1], [3,4]). true Now
I have the following lines of code from my homework, which takes some numbers.
I have a homework assignment where I need to take input from a file
The problem is like this: I have an array of 500 pointers which point
I have a homework assignment which asks to have the user input a date
As question states, I am doing homework which have 2 variables BOOK, and MAGAZINE
I have a homework and I need to evaluate which approach is better according
newbie doing Java homework here. I have one class named Album which contains the
I'm a newbie with UML and Microsoft Visio. Today, I have a homework which
Have a homework assignment in which I'm supposed to create a vector of pointers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.