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

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 15

void insert(char[]);
void display();
int count(struct listnode*, int);

struct listnode {
   char symbol[SIZE+1];
   struct listnode *next;
};

struct listnode *head = NULL;


main(){
   insert(\"a\");
   insert(\"ab\");
   insert(\"abc\");
   insert(\"abcd\");
   insert(\"abcde\");
   printf(\"%d\ \",count(head,3));
}


void insert(char symbol[]){
   if(head == NULL){
       head = (struct listnode*)malloc(sizeof(listnode));
       strcpy(head -> symbol,symbol);
       head -> next = NULL;
   }
   else {
       struct listnode *current = head;
       while(current -> next != NULL){
           current = current -> next;
       }
       current -> next = (struct listnode*) malloc(sizeof(listnode));
       strcpy(current->next->symbol,symbol);
       current -> next -> next = NULL;
   }
}

void display(){
   struct listnode *current = head;
   while(current != NULL){
       printf(\"%s\ \",current->symbol);
       current = current -> next;
   }
}

int count(struct listnode* p, int maxlen){
   struct listnode *current = p;
   int numofNodes = 0;
   while(current != NULL){
       if(strlen(current->symbol) <= maxlen){
           numofNodes++;
       }
       current = current -> next;
   }
   return numofNodes;
}

 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
 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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site