C Program to Print All Nodes of Linked List without Recursion

«
»
This C program, using iteration, 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.  * C Program to Display all the Nodes in a Linked List without using 
  3.  * 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.     display(head);
  24.     delete(&head);
  25.     return 0;
  26. }
  27.  
  28. void generate(struct node **head)
  29. {
  30.     int num = 10, i;
  31.     struct node *temp;
  32.  
  33.     for (i = 0; i < num; i++)
  34.     {
  35.         temp = (struct node *)malloc(sizeof(struct node));
  36.         temp->a = i;
  37.         if (*head == NULL)
  38.         {
  39.             *head = temp;
  40.             (*head)->next = NULL;
  41.         }
  42.         else
  43.         {
  44.             temp->next = *head;
  45.             *head = temp;
  46.         }
  47.     }
  48. }
  49.  
  50. void display(struct node *head)
  51. {
  52.     while (head != NULL)
  53.     {
  54.         printf("%d   ", head->a);
  55.         head = head->next;
  56.     }
  57.     printf("\n");
  58. }
  59.  
  60. void delete(struct node **head)
  61. {
  62.     struct node *temp;
  63.     while (*head != NULL)
  64.     {
  65.         temp = *head;
  66.         *head = (*head)->next;
  67.         free(temp);
  68.     }
  69. }

$ gcc linkdisplay.c -o linkdisplay
$ a.out
9   8   7   6   5   4   3   2   1   0

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
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.