Question: USING C - Problem trying to get a new node attached to end of linked list. Here's function: struct students * insert_back(struct students * start)

USING C - Problem trying to get a new node attached to end of linked list. Here's function:

struct students * insert_back(struct students * 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;

struct students * head;

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

//Origin of linkedlist is placed here head = start;

while(start->next != NULL){ start = start->next; } start->next = temp;

return head; }

One question I'd like to add before explaining further:

How does returning head somehow attach the additional node to it's tail? I got this function from my class's notes...but it doesn't make sense. What is going on in this function is that I'm taking the parameter, and moving it to it's null position, and placing an equivalent structure, in size, into that next position, and assigning THAT next position as null. When returning this function...all I can see is that it returns the head, which was equal to the parameter intially, so essentially, nothing is done.

I guess if my assumption^ is correct that would explain why this is happening:

When it returns head, after printing the first name in the beginning node, i equal my head node to the next node, and then print that first name out as well. The out gives me:

first name

(null)

Now, I tried:

head->next = start

return head;

but it didn't work at all. it would skip some user inputs as well.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!