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

This C program, using iteration, 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 without 
  3.  * using Recursion
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. struct node
  9. {
  10.     int a;
  11.     struct node *next;
  12. };
  13.  
  14. void generate(struct node **);
  15. void display(struct node *);
  16. void delete(struct node **);
  17.  
  18. int main()
  19. {
  20.     struct node *head = NULL;
  21.  
  22.     generate(&head);
  23.     printf("\nDisplaying the alternate nodes\n");
  24.     display(head);
  25.     delete(&head);
  26.  
  27.     return 0;
  28. }
  29.  
  30. void display(struct node *head)
  31. {
  32.     int flag = 0;
  33.  
  34.     while(head != NULL)
  35.     {
  36.         if (!(flag % 2))
  37.         {
  38.            printf("%d  ", head->a);
  39.         }
  40.         flag++;
  41.         head = head->next;
  42.     }
  43. }
  44.  
  45. void generate(struct node **head)
  46. {
  47.     int num, i;
  48.     struct node *temp;
  49.  
  50.     printf("Enter length of list: ");
  51.     scanf("%d", &num);
  52.     for (i = num; i > 0; i--)
  53.     {
  54.         temp = (struct node *)malloc(sizeof(struct node));
  55.         temp->a = i;
  56.         if (*head == NULL)
  57.         {
  58.             *head = temp;
  59.             (*head)->next = NULL;
  60.         }
  61.         else
  62.         {
  63.             temp->next = *head;
  64.             *head = temp;
  65.         }
  66.     }
  67. }
  68.  
  69. void delete(struct node **head)
  70. {
  71.     struct node *temp;
  72.     while (*head != NULL)
  73.     {
  74.         temp = *head;
  75.         *head = (*head)->next;
  76.         free(temp);
  77.     }
  78. }}

$ gcc alter_iter.c -o alter_iter
$ a.out
Enter length of list: 20
 
Displaying the alternate nodes
1  3  5  7  9  11  13  15  17  19

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.