This C Program uses recursive function & 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.
/*
* Recursive C program to find length of a linked list
*/
#include <stdio.h>
int find_len (char [], int);
int main ()
{
char str[100];
int len = 0;
printf ("Enter the string: \n");
scanf ("%[^\n]s", str);
len = find_len (str, 0);
printf ("The length of the given string is: %d\n", len);
return 0;
}
int find_len (char str[], int index)
{
static int l = 0;
if (str[index] == '\0')
return l;
else
l ++;
find_len (str, index + 1);
}
Enter the string: Sanfoundry C Programming The length of the given string is: 24 Enter the string: Programming Examples The length of the given string is: 20
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 Strings, go to C Programming Examples on Strings. If you wish to look at programming examples on all topics, go to C Programming Examples.
Next Steps:
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Apply for C Internship
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Buy C Books
- Watch Advanced C Programming Videos