Can some please help me write a C program that reads integers from the keyboard. The program should then build a linked list in the heap. Each node of the list contains an integer and a pointer to the next node in the list. The final node in the list has a null pointer. Your program should insert integers into the front of the list. After end-of-file has been generated by the user, your program uses the list to print out the integers in reverse order of how they were entered.
Explanation / Answer
#include #include struct node{ int data; struct node *next; }; struct node* add(struct node *head, int data){ struct node *tmp; tmp=(struct node *)malloc(sizeof(struct node)); if(tmp == NULL){ printf("Error! memory is not available "); exit(0); } tmp-> data = data; tmp-> next = head; return tmp; } void printlist(struct node *head) { if (head == NULL) return; printf("%d ", head->data); printlist(head->next); } int main() { struct node *head = NULL; head = add(head,1); /* 1 */ head = add(head,20);/* 20 1 */ head = add(head,10);/* 10 20 1 */ head = add(head,5); /* 5 10 20 1 */ printlist(head); return 0; }