Hi,
I'm writing a program that will use linked lists to print the
number of unique words, total words, and the most frequent word from a text file. I've gotten a decent amount of it done except for the linked lists. I got a start on the lists, but am still unsure where to proceed from there.
Below is the code:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct list_elem {
char *word;
int count;
struct list_elem *next;
};
struct list_elem *head;
int get_word(char *);
void add_to_list(char *);
void list_walk(void);
int unique_words, total_words;
struct list_elem *most_frequent_elem;
main() {
char word_buff[100];
while(get_word(word_buff))
add_to_list(word_buff);
list_walk();
printf("there are %d unique words out of %d total words\n",
unique_words, total_words);
printf("the most frequent word is <%s> which used %d times\n",
most_frequent_elem->word, most_frequent_elem->count);
}
void add_to_list(char *word_buff) {
//SECTION TO SEARCH THE LINKED LIST AND COUNT ANY
REPEATED WORDS
//MY MEASLY START, NOT SURE IF CORRECT
count=malloc(strlen(word(buff)+1));
/* ***** MAKE NEW ELEMENTS FOR BRAND NEW WORDS *****/
//WHAT I'VE GOTTEN SO FAR
sizeof(struct list_elem);
malloc(sizeof(list_elem));
}
void list_walk() {
/* ****** CODE TO WALK THE LIST AND GATHER THE STATS ***** */
struct list_elem *lp; int n=0;
for(lp=head; lp!=NULL; lp=lp->next)
n++;
}
//CODE DOESN'T NEED MODIFIED, I USED IT FROM AN OLD PROGRAM THAT TELLS THE COMPILER WHAT A WORD IS
#include <ctype.h>
int get_word(char *s) {
int c;
do {
c = getchar();
if(c == EOF)
return(0);
} while(!isalpha(c) && !isdigit(c));
do {
if(isupper(c))
c = tolower(c);
*s++ = c;
c = getchar();
} while(isalpha(c) || isdigit(c));
*s = 0;
return(1);
}
Any help is greatly appreciated.
Thanks,
James