C Program to Print Alternate Nodes of a Linked List using Recursion

«
»
This C program, using recursion, displays the alternate nodes in 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 in reverse. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Print the Alternate Nodes in a Linked List using Recursion
  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.     printf("\nDisplaying the alternate nodes\n");
  23.     display(head);
  24.     delete(&head);
  25.  
  26.     return 0;
  27. }
  28.  
  29. void display(struct node *head)
  30. {
  31.     static flag = 0;
  32.     if(head != NULL)
  33.     {
  34.         if (!(flag % 2))
  35.         {
  36.            printf("%d  ", head->a);
  37.         }
  38.         flag++;
  39.         display(head->next);
  40.     }
  41. }
  42.  
  43. void generate(struct node **head)
  44. {
  45.     int num, i;
  46.     struct node *temp;
  47.  
  48.     printf("Enter length of list: ");
  49.     scanf("%d", &num);
  50.     for (i = num; i > 0; i--)
  51.     {
  52.         temp = (struct node *)malloc(sizeof(struct node));
  53.         temp->a = i;
  54.         if (*head == NULL)
  55.         {
  56.             *head = temp;
  57.             (*head)->next = NULL;
  58.         }
  59.         else
  60.         {
  61.             temp->next = *head;
  62.             *head = temp;
  63.         }
  64.     }
  65. }
  66.  
  67. void delete(struct node **head)
  68. {
  69.     struct node *temp;
  70.     while (*head != NULL)
  71.     {
  72.         temp = *head;
  73.         *head = (*head)->next;
  74.         free(temp);
  75.     }
  76. }

$ gcc alter_display.c -o alter_display
$ a.out
Enter length of list: 10
 
Displaying the alternate nodes
1  3  5  7  9

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
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 & technical discussions at Telegram SanfoundryClasses.