C Program Find the Length of Linked List without Recursion

«
»
This C Program, using iteration, counts the number of 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 count the number of nodes in 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 find the Length of the Linked List without 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.  
  14. void generate(struct node **);
  15. int length(struct node*);
  16. void delete(struct node **);
  17.  
  18. int main()
  19. {
  20.     struct node *head = NULL;
  21.     int count;
  22.  
  23.     generate(&head);
  24.     count = length(head);
  25.     printf("The number of nodes are: %d\n", count);
  26.     delete(&head);
  27.  
  28.     return 0;
  29. }
  30.  
  31. void generate(struct node **head)
  32. {
  33.     /* for unknown number of nodes use num = rand() % 20; */
  34.     int num = 10, i;
  35.     struct node *temp;
  36.  
  37.     for (i = 0; i < num; i++)
  38.     {
  39.         temp = (struct node *)malloc(sizeof(struct node));
  40.         temp->a = i;
  41.         if (*head == NULL)
  42.         {
  43.             *head = temp;
  44.             (*head)->next = NULL;
  45.         }
  46.         else
  47.         {
  48.             temp->next = *head;
  49.             *head = temp;
  50.         }
  51.     }
  52. }
  53.  
  54. int length(struct node *head)
  55. {
  56.     int num = 0;
  57.     while (head != NULL)
  58.     {
  59.         num += 1;
  60.         head = head->next;
  61.     }
  62.     return num;
  63. }
  64.  
  65. void delete(struct node **head)
  66. {
  67.     struct node *temp;
  68.     while (*head != NULL)
  69.     {
  70.         temp = *head;
  71.         *head = (*head)->next;
  72.         free(temp);
  73.     }
  74. }

$ gcc numbernode.c -o numbernode
$ a.out
The number of nodes are: 10

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 & technical discussions at Telegram SanfoundryClasses.