Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions

Question

What is a kin-network system?

Answered: 1 week ago