Can someone help me write a C program that reads integers from the keyboard. The
ID: 3635884 • Letter: C
Question
Can someone 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 anull pointer. The program should insert integers into the front of the list. After end-of-file has been generated by the user, the program uses the list to print out the integers in reverse order of how they were typed in.
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; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.