#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wrdcnt2c.h"
extern wordlist *head;
wordlist *curr,*prev;
/*------------------INSERTW---------------------------*/
/* Determine if a word to be inserted in the word */
/* list is already in the list. If the word is in the */
/* list, increment count. If the word is not in */
/* the list, find the position in the list where it */
/* should be inserted. */
/* Language: C */
void insertw(char * wordarry) /* readin passes wordstrg */
{
int found=FALSE,test;
curr=head;
/* curr is now pointing to the start of the list */
/* While curr- next is not null(END of the */
/* list), and */
/* found is FALSE, search word list sequentially by */
/* comparing wordarry string to curr->word */
while((curr->next != NULL) && !found) {
prev=curr;
curr=curr->next;
test=strcmp(wordarry,curr->word);
/* If test is 0, strings are equal; increment count */
if (test == 0) {
curr->totcnt++;
found=TRUE;
}
/* If test is less than 0, wordarry is "alpha- */
/* betically less than" curr->word. Call enter, */
/* to insert word BEFORE curr->word */
else if (test < 0) {
enter(wordarry,BEFORE);
found=TRUE;
}
}
/* If found is FALSE, call enter to insert word at */
/* END of list */
if (!found)
enter(wordarry,END);
}
/*------------------ENTER-----------------------------*/
/* enter new word (after allocating space) into */
/* position determined by insertword */
/* */
void enter(char * wordarry, int position)
{
wordlist *new;
new=(wordlist *)malloc(sizeof(wordlist));
new->totcnt=1;
new->word=(char *)malloc(strlen(wordarry)+1);
strcpy(new->word,wordarry);
/* allocate space for "new" entry and */
/* initialize "new" data by setting count to 1 */
/* and copying wordarry into new->word
*/
/* If position is BEFORE, set prev->next to new; */
/* set new->next to curr: */
if (position==BEFORE) {
prev->next=new;
new->next=curr;
}
/* Otherwise, enter the word at the end of the */
/* list (END) */
else {
curr->next=new;
new->next=NULL;
}
}
/*------------------PRINTLST--------------------------*/
/* print the linked list using recursion */
/* output: total count and word */
/* */
void printlst(wordlist *head)
{
if (head == NULL)
return;
else {
printf("%3d %s\n",head->totcnt,
head->word);
printlst(head->next);
}
}
|