This C program allows you to find the length of a linked list, which is the number of nodes present in the list, without using recursion.
Problem Statement
You are given a linked list, and your task is to write a C program to calculate the length of the linked list without using recursion.
C Program Find the Length of Linked List without Recursion
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_SIZE 100
void removeRepeatedWords(char *str) {
char *words[MAX_SIZE];
int count = 0;
// Tokenize the string into words
char *token = strtok(str, " ");
while (token != NULL) {
words[count++] = token;
token = strtok(NULL, " ");
}
// Check for repeated words and remove them
for (int i = 0; i < count; i++) {
if (words[i] != NULL) {
for (int j = i + 1; j < count; j++) {
if (words[j] != NULL && strcmp(words[i], words[j]) == 0) {
free(words[j]);
words[j] = NULL;
}
}
}
}
// Reconstruct the string without repeated words
int len = 0;
for (int i = 0; i < count; i++) {
if (words[i] != NULL) {
int wordLen = strlen(words[i]);
memcpy(str + len, words[i], wordLen);
len += wordLen;
str[len++] = ' ';
free(words[i]);
}
}
str[len - 1] = '\0'; // Remove the trailing space
}
int main() {
char str[MAX_SIZE];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Remove the newline character at the end of the input
str[strcspn(str, "\n")] = '\0';
removeRepeatedWords(str);
printf("String after removing repeated words: %s\n", str);
return 0;
}
How it Works
- The program begins by including the necessary header files:
stdio.hfor input/output functions,string.hfor string manipulation functions, andstdlib.hfor memory allocation functions. - The
MAX_SIZEconstant is defined, which represents the maximum size of the input string. - The
removeRepeatedWordsfunction is defined. This function takes a pointer to a character array (string) as its parameter. - Inside the
removeRepeatedWordsfunction:- An array of character pointers called
wordsis declared to store the individual words from the string. - An integer variable
countis initialized to keep track of the number of words stored in thewordsarray.
- An array of character pointers called
- The string is tokenized into words using the
strtokfunction. Thestrtokfunction splits the input string based on a specified delimiter, which in this case is a space character (” “). Each word is then stored in thewordsarray, and thecountis incremented. - The program iterates through the
wordsarray to check for repeated words. It compares each word with the remaining words in the array using thestrcmpfunction.- If a duplicate word is found, the corresponding pointer in the
wordsarray is freed using thefreefunction, and the pointer is set toNULL. - This step ensures that the duplicate word is removed from the array but doesn’t modify the original string.
- If a duplicate word is found, the corresponding pointer in the
- After removing the duplicate words, the program reconstructs the string without the repeated words. It uses the
memcpyfunction to copy each non-null word from thewordsarray to the original string (str), updating thelenvariable to keep track of the string length. A space character is added after each word to separate them. - Once all the non-repeated words are copied, the trailing space character is replaced with a null character (‘\0’) to properly terminate the string.
- In the
mainfunction:- The user is prompted to enter a string.
- The input string is read using
fgetsand stored in thestrarray. - The newline character at the end of the input string is removed by replacing it with a null character (‘\0’) using
strcspn. - The
removeRepeatedWordsfunction is called to remove repeated words from the input string. - Finally, the resulting string after removing repeated words is printed as output.
Input /Output

Explanation:
- The linked list has 5 nodes, so the length of the list is 5.
- The
findLengthfunction is called with the head of the list as an argument. - Inside the function, the while loop iterates through the linked list, incrementing the
lengthvariable by 1 for each node. - After traversing the entire list, the function returns the final value of
length. - The
mainfunction captures the returned length and prints it to the console usingprintf, resulting in the output: “Length of the linked list is: 5”.