C Program to Print All Nodes of Linked List using Recursion

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 <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. struct node
  8. {
  9.     int a;
  10.     struct node *next;
  11. };
  12.  
  13. void generate(struct node **);
  14. void display(struct node*);
  15. void delete(struct node **);
  16.  
  17. int main()
  18. {
  19.     struct node *head = NULL;
  20.  
  21.     generate(&head);
  22.     display(head);
  23.     delete(&head);
  24.     return 0;
  25. }
  26.  
  27. void generate(struct node **head)
  28. {
  29.     int num = 10, i;
  30.     struct node *temp;
  31.  
  32.     for (i = 0; i < num; i++)
  33.     {
  34.         temp = (struct node *)malloc(sizeof(struct node));
  35.         temp->a = i;
  36.         if (*head == NULL)
  37.         {
  38.             *head = temp;
  39.             (*head)->next = NULL;
  40.         }
  41.         else
  42.         {
  43.             temp->next = *head;
  44.             *head = temp;
  45.         }
  46.     }
  47. }
  48.  
  49. void display(struct node *head)
  50. {
  51.     printf("%d    ", head->a);
  52.     if (head->next == NULL)
  53.     {
  54.         return;
  55.     }
  56.     display(head->next);
  57. }
  58.  
  59. void delete(struct node **head)
  60. {
  61.     struct node *temp;
  62.     while (*head != NULL)
  63.     {
  64.         temp = *head;
  65.         *head = (*head)->next;
  66.         free(temp);
  67.     }
  68. }

$ cc pgm15.c
$ a.out
9    8    7    6    5    4    3    2    1    0

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best 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.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.