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

Write a function insertAtEnd() for a linked list that has the following declarat

ID: 3562177 • Letter: W

Question

Write a function insertAtEnd() for a linked list that has the following declaration:

void insertAtEnd (struct node **pStart, int myData);

The function should insert a new node, at the end of the linked list, with the data value myData. Assume that struct node is defined as follows:

struct node

{

     int data;

     struct node *pNext;

};

You may also assume that the function :

struct node *makeNode(int myData)

has already been defined. Recall that makeNode() allocates a node dynamically and sets the node

Explanation / Answer

#include #include // Structure for the list element/node struct node { int data; // Stores the data struct node *next; // Points to the next element in the list. }; int InsertAtEnd(struct node **, struct node **, int); /*Declaration of the function which inserts elements at the end.*/ int main() { struct node *HEAD=NULL; //Points to the first element in the list. struct node *LAST=NULL; //Points to the last element in the list. int i=1; for(i=1;idata=i; // Assign value to the data variable of the newnode. newnode->next=NULL; // Assign NULL to the next pointer of the newnode. if(*headref==NULL) //Checks if the list is empty. { *headref=newnode; // Places the address of the new node in HEAD pointer. *lastref=newnode; // Places the address of the new node in LAST pointer. return 0; //Exit function } /* If the list is not empty, then make the next pointer of the present last node point to the new node*/ (*lastref)->next=newnode; *lastref=(*lastref)->next; // Increment LAST to point to the new last node. return 0; }
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