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
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started