In: Computer Science
In C, write a function that inserts a new node by taking four arguments, a head, tail, current node, and new node pointers. If head is NULL, perform head insertion, otherwise transverse the linked list recursively by using current node pointer. Terminate when the current node points at the tail after inserting new node at tail and pointing tail at new node.
void insert(node *head, node *tail, node *current, node *new){
//if
head pointer is NULL, assign new node to head
if(head == NULL)
{
head = new;
head->next = NULL; //only node in the
linkedlist
return //terminate function
}
if(current == tail)
//tail
node reached, perform insertion
{
tail->next = new;
tail = new;
tail->next = NULL; //last node in the linked
list
return;
//terminate function
}
//recursivery traverse the
linkedlist untill we reach the tail node
insert(head, tail, current->next; new);
}