Free linked list recursively C++

This C Program uses recursive function & displays a linked list. A linked list is an ordered set of data elements, each containing a link to its successor.

Here is the source code of the C program to display a linked list. The C Program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2. * Recursive C program to display members of a linked list
  3. */
  4. #include
  5. #include
  6. struct node
  7. {
  8. int a;
  9. struct node *next;
  10. };
  11. void generate[struct node **];
  12. void display[struct node*];
  13. void delete[struct node **];
  14. int main[]
  15. {
  16. struct node *head = NULL;
  17. generate[&head];
  18. display[head];
  19. delete[&head];
  20. return 0;
  21. }
  22. void generate[struct node **head]
  23. {
  24. int num = 10, i;
  25. struct node *temp;
  26. for [i = 0; i a = i;
  27. if [*head == NULL]
  28. {
  29. *head = temp;
  30. [*head]->next = NULL;
  31. }
  32. else
  33. {
  34. temp->next = *head;
  35. *head = temp;
  36. }
  37. }
  38. }
  39. void display[struct node *head]
  40. {
  41. printf["%d ", head->a];
  42. if [head->next == NULL]
  43. {
  44. return;
  45. }
  46. display[head->next];
  47. }
  48. void delete[struct node **head]
  49. {
  50. struct node *temp;
  51. while [*head != NULL]
  52. {
  53. temp = *head;
  54. *head = [*head]->next;
  55. free[temp];
  56. }
  57. }
advertisement
$ cc pgm15.c $ a.out 9 8 7 6 5 4 3 2 1 0

Sanfoundry Global Education & Learning Series 1000 C Programs.

Subscribe Now: C Programs Newsletter | Important Subjects Newsletters
advertisement
advertisement

Heres the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Linked List, go to Linked List. If you wish to look at programming examples on all topics, go to C Programming Examples.
« Prev - C Program to Find Area of Parallelogram
» Next - C Program to Print Armstrong Number from 1 to 1000
Next Steps:

  • Get Free Certificate of Merit in C Programming
  • Participate in C Programming Certification Contest
  • Become a Top Ranker in C Programming
  • Take C Programming Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:

  • Apply for C Internship
  • Buy C Books
  • Practice BCA MCQs
  • Practice Computer Science MCQs
  • Buy Computer Science Books

Video liên quan

Chủ Đề