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

Write a function that will take the following parameters: head_ref val - an inte

ID: 3585390 • Letter: W

Question

Write a function that will take the following parameters:

head_ref

val - an integer value to be appended to the end of the linked list

Your function will create a new node with the value in the parameter val and add the node to the end of an existing linked list passed in to the function as head.

If the head is null, set the head pointer to the new node.

Your function will return the head pointer of the updated linked list.

Below is the structure for a node. It is already defined before your code runs.

The linked list structure:

For example:

Test Result
  // List is: 5->4->3->2  //val =1  
  5->4->3->2->1  

Explanation / Answer

Element* append(Element* head_ref, int val)

{

// create a new element to be inserted at end

struct Element* element = (struct Element*) malloc(sizeof(struct Element));

element->val = val;

element->next = NULL;

// check if list is empty

if (head_ref == NULL)

{

return element;

}

  

struct Element *last = head_ref;

// traverse list to reach end

while (last->next != NULL)

last = last->next;

  

// add node at last

last->next = element;

return head_ref;

}

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