Users Online

· Guests Online: 43

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C Program to Display all the Nodes in a Linked List using Recursion

C Program to Display all the Nodes in a 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

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.74 seconds
10,272,896 unique visits