11/*
22 * Write a program that prints the distinct words in its input sorted into
33 * decreasing order of frequency of occurrence. Precede each word by its count.
4+ *
45 * By Faisal Saadatmand
56 */
67
@@ -21,17 +22,17 @@ struct tnode {
2122
2223/* functions */
2324int getword (char * , int );
24- struct tnode * talloc (void ); /* allocate memory to new tree node */
25- char * strDup (char * ); /* copy string into safe place */
25+ struct tnode * talloc (void ); /* allocate memory to new tree node */
26+ char * strDup (char * ); /* copy string into safe place */
2627struct tnode * addtree (struct tnode * , char * );
2728void treeprint (struct tnode * );
2829struct tnode * copyTree (struct tnode * , struct tnode * );
2930struct tnode * sortTree (struct tnode * , struct tnode * );
3031struct tnode * freetree (struct tnode * );
3132
3233/* globals */
33- int buf [BUFSIZE ]; /* buffer from ungetch */
34- int bufp = 0 ; /* next free position in buf */
34+ int buf [BUFSIZE ]; /* buffer from ungetch */
35+ int bufp = 0 ; /* next free position in buf */
3536
3637int getch (void ) /* get a (possibly pushed back) character */
3738{
@@ -45,6 +46,7 @@ void ungetch(int c) /* push character back on input */
4546 else
4647 buf [bufp ++ ] = c ;
4748}
49+
4850/* getword: get next word or character from input */
4951int getword (char * word , int lim )
5052{
@@ -72,16 +74,16 @@ int getword(char *word, int lim)
7274/* talloc: make a tnode */
7375struct tnode * talloc (void )
7476{
75- return ( struct tnode * ) malloc (sizeof (struct tnode ));
77+ return malloc (sizeof (struct tnode ));
7678}
7779
7880/*strDup: make a duplicate of s */
7981char * strDup (char * s )
8082{
8183 char * p ;
8284
83- p = ( char * ) malloc (strlen (s ) + 1 ); /* +1 for '\0' */
84- if (p != NULL )
85+ p = malloc (strlen (s ) + 1 ); /* +1 for '\0' */
86+ if (p )
8587 strcpy (p , s );
8688 return p ;
8789}
@@ -91,7 +93,7 @@ struct tnode *addtree(struct tnode *p, char *w)
9193{
9294 int cond ;
9395
94- if (p == NULL ) { /* a new word has arrived */
96+ if (! p ) { /* a new word has arrived */
9597 p = talloc (); /* make a new node */
9698 p -> word = strDup (w ); /* copy data to it */
9799 p -> count = 1 ;
@@ -108,7 +110,7 @@ struct tnode *addtree(struct tnode *p, char *w)
108110/* print: in-order print of tree p */
109111void treeprint (struct tnode * p )
110112{
111- if (p != NULL ) {
113+ if (p ) {
112114 treeprint (p -> right );
113115 printf ("%4d %s\n" , p -> count , p -> word );
114116 treeprint (p -> left );
@@ -118,7 +120,7 @@ void treeprint(struct tnode *p)
118120/* copyTree: copy nodes in root into p according to frequency of occurrence. */
119121struct tnode * copyTree (struct tnode * p , struct tnode * root )
120122{
121- if (p == NULL ) {
123+ if (! p ) {
122124 p = talloc ();
123125 p -> word = strDup (root -> word );
124126 p -> count = root -> count ;
@@ -134,7 +136,7 @@ struct tnode *copyTree(struct tnode *p, struct tnode *root)
134136 * to frequency of occurrence */
135137struct tnode * sortTree (struct tnode * p , struct tnode * root )
136138{
137- if (root != NULL ) {
139+ if (root ) {
138140 p = sortTree (p , root -> left );
139141 p = copyTree (p , root );
140142 p = sortTree (p , root -> right );
@@ -145,28 +147,30 @@ struct tnode *sortTree(struct tnode *p, struct tnode *root)
145147/* freetree: free allocated heap memory of node tree */
146148struct tnode * freetree (struct tnode * node )
147149{
148- if (node != NULL ) {
150+ if (node ) {
149151 freetree (node -> left );
150152 freetree (node -> right );
151153 free (node -> word );
152154 free (node );
153155 }
154156 return node ;
155157}
158+
156159int main (void )
157160{
158161 struct tnode * root ; /* root node */
159- struct tnode * sRoot ; /* root node to sorted tree */
162+ struct tnode * sorted ; /* root node to sorted tree */
160163 char word [MAXWORD ]; /* currently read word */
161164
162- root = sRoot = NULL ;
163-
165+ root = sorted = NULL ;
164166 while (getword (word , MAXWORD ) != EOF )
165167 if (isalpha (word [0 ]))
166- root = (addtree (root , word ));
167- sRoot = sortTree (sRoot , root );
168- treeprint (sRoot );
169- root = freetree (root ); /* clean up */
170- sRoot = freetree (sRoot ); /* clean up */
168+ root = (addtree (root , word )); /* build tree */
169+ sorted = sortTree (sorted , root );
170+ treeprint (sorted );
171+ /* clean up */
172+ root = freetree (root );
173+ sorted = freetree (sorted );
174+ root = sorted = NULL ;
171175 return 0 ;
172176}
0 commit comments