This problem involves a linked list where each node stores a
This problem involves a linked list where each node stores a symbol (a string of length at most 15) along with a pointer to the next element. Thus, the struct definition for each node of the list is as follows: #define SIZE 15 struct listnode {char symbol[SIZE+1]; struct listnode *next;}; Write a function int count (struct listnode *p, int maxlen), where parameter p is a pointer to the head of the list. Your function must return the number of nodes in the list where the length of the string stored is at most the value given by parameter maxlen. You need to show only the C code for the above function. You need to perform basic error checking. You don\'t need to include comments in your code.
Solution
// C code count number of nodes in linked list
int count(struct listnode *p, int maxlen)
{
// check for empty list
if(p == NULL)
return 0;
// create temporary node
struct listnode *temporary = p;
int numberOfNodes = 0;
// go till the end of list and count the nodes
while(temporary!=NULL)
{
numberOfNodes++;
// go to the next node
temporary=temporary->next;
}
// return the number of nodes found
return numberOfNodes;
}
