You are killing yourself by copying bad code from one project to
another without letting any of the lessons of the previous projects
sink in.
okay, I have took time and did correct the mistakes you mentioned. I am
not going further to write other functions yet. First, I am posting the
corrected code for a check by you folks:
/* K&R2, exercise 6.4
*
* write a program that prints the distinct words in its input sorted into
* decreasing order of frequency of occurence. Precede each word by its
count.
*
*
* The basic 2 step design-idea:
*
* (1) we are going to create a linked list and store words in
it.
* Each structure will have a count associated with the word it carries.
When
* we will see a word which is already in the list then we will increase
the count
* by 1. That way we will have a linked-list of unique structures.
*
* (2) we will save a pointer to each element of the 1st array
( which
* are structures) in an array of pointers and will sort these pointers
according
* to the number of occurences of each word. This will make program
efficient
* as we will not be sorting the structures in linked-list but the
pointers
* to those structures.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
enum MAXSIZE { ARRSIZE = 1000, WORDSIZE = 30 };
int getword( char * , int );
struct snode *add_to_snode( struct snode *, char * );
void print_snode( struct snode ** );
char *strdup( char * );
struct snode
{
char *word;
int count;
struct snode *next;
};
int main( void )
{
char word[WORDSIZE];
struct snode *root_node;
struct snode *arr_psnodes[ARRSIZE];
struct snode **psnode, **psnode_last;
psnode = arr_psnodes;
psnode_last = psnode + (ARRSIZE - 1);
root_node = NULL;
while( getword( word, WORDSIZE ) && (psnode != psnode_last) )
{
if( isalpha(word[0]) )
{
root_node = add_to_snode( root_node, word );
*(psnode++) = root_node;
}
}
print_snode( arr_psnodes );
return 0;
}
struct snode *add_to_snode( struct snode *p, char *w )
{
int match;
if( p == NULL )
{
p = malloc( sizeof(struct snode));
if( !p )
{
fprintf( stderr, "out of memory\n" );
exit( EXIT_FAILURE );
}
p->word = strdup( w );
p->count = 1;
p->next = NULL;
}
else if ( !(match = strcmp( p->word, w )) )
{
p->count++;
}
else if( match )
{
p->next = add_to_snode( p->next, w );
}
return p;
}
void print_snode( struct snode **pp )
{
/* yet to be done */
}
void sort_nodes( struct snode **pp )
{
/* yet to be done */
}
char *strdup( char *pc )
{
char *dup;
dup = malloc( strlen(pc) + 1 );
if( dup )
{
strcpy( dup, pc );
}
return dup;
}
/* A program that takes a single word as input. It will discard
* the whole input if it contains anything other than the 26 alphabets
* of English. If the input word contains more than 30 letters then only
* the extra letters will be discarded . For general purpose usage of
* English it does not make any sense to use a word larger than this size.
* Nearly every general purpose word can be expressed in a word with less
* than or equal to 30 letters.
*
*/
int getword( char *word, int max_length )
{
int c;
char *w = word;
while( isspace( c = getchar() ) )
{
;
}
while( --max_length )
{
if( isalpha( c ) )
{
*w++ = c;
}
else if( isspace( c ) || c == EOF )
{
*w = '\0';
break;
}
else
{
return 0;
}
c = getchar();
}
*w = '\0';
return word[0];
}