Skip to content

Commit bc411df

Browse files
committed
minor tweaks
1 parent f1fd3b4 commit bc411df

File tree

1 file changed

+71
-74
lines changed

1 file changed

+71
-74
lines changed

Diff for: chapter06/6-3.c

+71-74
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,47 @@
66
* By Faisal Saadatmand
77
*/
88

9-
#include <stdio.h>
10-
#include <string.h>
119
#include <ctype.h>
10+
#include <stdio.h>
1211
#include <stdlib.h>
12+
#include <string.h>
1313

1414
#define MAXWORD 100
1515
#define BUFSIZE 100
1616
#define NKEYS (sizeof noisetab / sizeof noisetab[0])
1717

18+
/* types */
19+
struct list {
20+
int number;
21+
struct list *next;
22+
};
23+
1824
struct tnode {
1925
char *word;
20-
struct list *line;
21-
int count;
26+
struct list *lines;
2227
struct tnode *left;
2328
struct tnode *right;
2429
};
2530

26-
struct list {
27-
int number;
28-
struct list *next;
29-
};
30-
3131
struct key {
3232
char *word;
3333
int count;
3434
};
3535

36+
/* functions */
37+
int getword(char *, int);
38+
struct tnode *talloc(void); /* allocate memory to new tree node */
39+
char *strDup(char *); /* copy string into safe place */
40+
struct tnode *addtree(struct tnode *, char *, int);
41+
void treeprint(struct tnode *);
42+
void printList(struct list *);
43+
struct key *binsearch(char *, struct key *, int);
44+
void freetree(struct tnode *);
45+
46+
/* globals */
47+
int buf[BUFSIZE]; /* buffer from ungetch */
48+
int bufp = 0; /* next free position in buf */
49+
3650
struct key noisetab[] = {
3751
{ "a", 0 },
3852
{ "and", 0 },
@@ -46,38 +60,22 @@ struct key noisetab[] = {
4660
{ "with", 0},
4761
};
4862

49-
/* functions */
50-
int getword(char *, int, int *);
51-
struct tnode *talloc(void); /* allocate memory to new tree node */
52-
char *strDup(char *); /* copy string into safe place */
53-
struct tnode *addtree(struct tnode *, char *, int);
54-
void treeprint(struct tnode *);
55-
void printList(struct list *);
56-
struct key *binsearch(char *, struct key *, int);
57-
struct tnode *freetree(struct tnode *);
58-
struct list *freelist(struct list *);
59-
60-
/* globals */
61-
int buf[BUFSIZE]; /* buffer from ungetch */
62-
int bufp = 0; /* next free position in buf */
63-
6463
/* getword: get next word or character from input */
65-
int getword(char *word, int lim, int *ln)
64+
int getword(char *word, int lim)
6665
{
6766
int c, getch(void);
6867
void ungetch(int);
6968
char *w = word;
7069

71-
while (isspace(c = getch()))
72-
if (c == '\n')
73-
(*ln)++;
70+
while (isblank(c = getch()))
71+
;
7472
if (c != EOF)
7573
*w++ = c;
7674
if (!isalpha(c)) {
7775
*w = '\0';
7876
return c;
7977
}
80-
for ( ; --lim > 0; w++)
78+
for ( ; --lim > 0; ++w)
8179
if (!isalnum(*w = getch())) {
8280
ungetch(*w);
8381
break;
@@ -86,12 +84,14 @@ int getword(char *word, int lim, int *ln)
8684
return word[0];
8785
}
8886

89-
int getch(void) /* get a (possibly pushed back) character */
87+
/* get a (possibly pushed back) character */
88+
int getch(void)
9089
{
9190
return (bufp > 0) ? buf[--bufp] : getchar();
9291
}
9392

94-
void ungetch(int c) /* push character back on input */
93+
/* push character back on input */
94+
void ungetch(int c)
9595
{
9696
if (bufp >= BUFSIZE)
9797
printf("ungetch: too many characters\n");
@@ -119,13 +119,12 @@ char *strDup(char *s)
119119
/* addList: add a node with ln, at or before p */
120120
struct list *addlist(struct list *p, int ln)
121121
{
122-
if (p == NULL) {
122+
if (!p) {
123123
p = malloc(sizeof(struct list));
124124
p->number = ln;
125125
p->next = NULL;
126126
} else if (p->number != ln) /* skip multi-occurrence on same line */
127127
p->next = addlist(p->next, ln);
128-
129128
return p;
130129
}
131130

@@ -135,16 +134,14 @@ struct tnode *addtree(struct tnode *p, char *w, int ln)
135134
int cond;
136135
struct list *first = NULL;
137136

138-
if (p == NULL) { /* a new word has arrived */
137+
if (!p) { /* a new word has arrived */
139138
p = talloc(); /* make a new node */
140139
p->word = strDup(w); /* copy data to it */
141-
p->count = 1;
142-
p->line = addlist(first, ln);
140+
p->lines = addlist(first, ln);
143141
p->left = p->right = NULL;
144-
} else if ((cond = strcmp(w, p->word)) == 0) {
145-
p->count++; /* repeated word */
146-
p->line = addlist(p->line, ln);
147-
} else if (cond < 0) /* less than into left subtree */
142+
} else if (!(cond = strcmp(w, p->word)))
143+
p->lines = addlist(p->lines, ln);
144+
else if (cond < 0) /* less than into left subtree */
148145
p->left = addtree(p->left, w, ln);
149146
else
150147
p->right = addtree(p->right, w, ln);
@@ -154,22 +151,22 @@ struct tnode *addtree(struct tnode *p, char *w, int ln)
154151
/* treeprint: in-order print of tree p */
155152
void treeprint(struct tnode *p)
156153
{
157-
if (p != NULL) {
158-
treeprint(p->left);
159-
printf("%4d %s ", p->count, p->word);
160-
printList(p->line);
161-
printf("\n");
162-
treeprint(p->right);
163-
}
154+
if (!p)
155+
return;
156+
treeprint(p->left);
157+
printf(" %s ", p->word);
158+
printList(p->lines);
159+
printf("\n");
160+
treeprint(p->right);
164161
}
165162

166163
/* printList: preorder print of list p */
167164
void printList(struct list *p)
168165
{
169-
if (p != NULL) {
170-
printf("%d ", p->number);
171-
printList(p->next);
172-
}
166+
if (!p)
167+
return;
168+
printf("%d ", p->number);
169+
printList(p->next);
173170
}
174171

175172
/* binsearch: find word in tab[0]...tab[n - 1] */
@@ -193,43 +190,43 @@ struct key *binsearch(char *word, struct key *tab, int n)
193190
}
194191

195192
/* freellist: frees allocated heap memory of linked list */
196-
struct list *freelist(struct list *node)
193+
void freelist(struct list *node)
197194
{
198-
if (node != NULL) {
199-
freelist(node->next);
200-
free(node);
201-
}
202-
return node;
195+
if (!node)
196+
return;
197+
freelist(node->next);
198+
free(node);
203199
}
204200

205201
/* freetree: frees allocated heap memory of tree */
206-
struct tnode *freetree(struct tnode *node)
202+
void freetree(struct tnode *node)
207203
{
208-
if (node != NULL) {
209-
freetree(node->left);
210-
freetree(node->right);
211-
free(node->word);
212-
freelist(node->line); /* delete linked list in nodes */
213-
free(node);
214-
}
215-
return node;
204+
void freelist(struct list *);
205+
206+
if (!node)
207+
return;
208+
freetree(node->left);
209+
freetree(node->right);
210+
free(node->word);
211+
freelist(node->lines); /* delete linked list in nodes */
212+
free(node);
216213
}
217214

218215
int main(void)
219216
{
220217
struct tnode *root; /* root node */
221-
struct key *sought; /* currently sought after word */
222218
char word[MAXWORD]; /* currently read word */
223-
int lineNo = 1; /* currently searched line */
219+
int lineno = 1; /* currently searched line */
224220

225221
root = NULL;
226-
while (getword(word, MAXWORD, &lineNo) != EOF) {
227-
sought = binsearch(word, noisetab, NKEYS); /* skip noise words */
228-
if (isalpha(word[0]) && !sought)
229-
root = addtree(root, word, lineNo);
230-
}
222+
while (getword(word, MAXWORD) != EOF)
223+
if (isalpha(word[0]) && !binsearch(word, noisetab, NKEYS))
224+
root = addtree(root, word, lineno);
225+
else if (word[0] == '\n')
226+
++lineno;
231227
treeprint(root);
232-
root = freetree(root); /* clean up */
228+
/* clean up */
229+
freetree(root);
233230
root = NULL;
234231
return 0;
235232
}

0 commit comments

Comments
 (0)