Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Trying to recursively add linked nodes to my head node but it just continuously

ID: 3798815 • Letter: T

Question

Trying to recursively add linked nodes to my head node but it just continuously loops and doesn't stop when I need it to:

struct students * insert_back(struct students * start, int numstudents){

numstudents = numstudents--;

if(numstudents==0){
return start;
}
struct students * temp = calloc(1,sizeof(struct students));
temp->firstName = calloc(29, sizeof(char));
temp->lastName = calloc(29, sizeof(char));
temp->birthMonth = calloc(9, sizeof(char));
temp->next = NULL;

fscanf(ifp, "%s", temp->firstName);
printf("%s " , temp->firstName);
fscanf(ifp, "%s", temp->lastName);
printf("%s " , temp->lastName);
fscanf(ifp, "%s", temp->birthMonth);
printf("%s " , temp->birthMonth);
fscanf(ifp, "%d", &temp->numDay);
fscanf(ifp, "%d", &temp->numYear);

struct students * head;

head = start;

start->next = temp;

head->next = insert_back(start, numstudents);

return head;
}

Explanation / Answer

struct students * insert_back(struct students * start, int numstudents){

numstudents = numstudents--;

if(numstudents==0){
return start;
}
struct students * temp = calloc(1,sizeof(struct students));
temp->firstName = calloc(29, sizeof(char));
temp->lastName = calloc(29, sizeof(char));
temp->birthMonth = calloc(9, sizeof(char));
temp->next = NULL;

fscanf(ifp, "%s", temp->firstName);
printf("%s " , temp->firstName);
fscanf(ifp, "%s", temp->lastName);
printf("%s " , temp->lastName);
fscanf(ifp, "%s", temp->birthMonth);
printf("%s " , temp->birthMonth);
fscanf(ifp, "%d", &temp->numDay);
fscanf(ifp, "%d", &temp->numYear);

struct students * head;

head = start;

start->next = temp;

head->next = insert_back(start->next, numstudents);//u have to forward the start node to append nodes...

//try this..

return head;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote