Write a C function to add a node to the end of a linked list

Write a C++ function to add a node to the end of a linked list. The function takes two arguments-the head of the linked list and the value to be added. It should return the head of the linked list. node astir AddNode (node astir head, int key); The linked list structure: struct node {int key; node astir next;};

Solution

since main is not required onyl the function is required that takes two arguments, funcion is as follows: Node* Insert(Node *head,int data) { // creating a temprary which holds the last node and it also set last node data and next Node *last = new Node; last->data = data; last->next = NULL; // if the linked_list is empty then set head = last if (head == NULL) { head = last; } else { // if the linked list is not empty // creating a temprary node and sets it to head Node *temp = new Node; temp = head; // using temp to find the last node while (temp->next != NULL) { temp = temp->next; } // appending the last node with last temp->next = last; } // returning the head of linked list return head; }
 Write a C++ function to add a node to the end of a linked list. The function takes two arguments-the head of the linked list and the value to be added. It shou

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site