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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.