Question: This is a question about linked list in C. In the current insert function, I inserted the new node into the front and I want

This is a question about linked list in C. In the current insert function, I inserted the new node into the front and I want to insert the node from the end of the linked list without changing the function prototype. I tried the existing solution on stackoverflow, but it doesn't work. https://stackoverflow.com/questions/21565023/c-linked-list-inserting-node-at-the-end

#include #include

typedef struct node* nptr; typedef struct node { int data; nptr link; } NODE;

void Insert(nptr*, int); void PrintAll(nptr);

int main() { int A[10] = { 3, 9, 8, 2, 5, 10, 7, 1, 4, 6 }; nptr head = NULL; for (int i = 0; i < 10; ++i) Insert(&head, A[i]);

PrintAll(head); free(head); return 0; }

void Insert(nptr* p, int n) { nptr tmp = (nptr)malloc(sizeof(NODE)); tmp->data = n; tmp->link = *p; *p = tmp; }

void PrintAll(nptr head) { nptr tmp = head; while (tmp) { printf("%d ", tmp->data); tmp = tmp->link; } printf(" "); }

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!