Skip to content

Commit 897ee03

Browse files
committed
code cleanup
1 parent 10da787 commit 897ee03

File tree

2 files changed

+38
-35
lines changed

2 files changed

+38
-35
lines changed

chapter06/6-3.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
/* Exercise 6-3. Write a cross-referencer that prints a list of all words in a
1+
/*
2+
* Exercise 6-3. Write a cross-referencer that prints a list of all words in a
23
* document, and, for each word, a list of the line numbers on which it occurs.
34
* Remove noise words like "the," "and," and so on.
5+
*
46
* By Faisal Saadatmand
57
*/
68

@@ -100,15 +102,15 @@ void ungetch(int c) /* push character back on input */
100102
/* talloc: make a tnode */
101103
struct tnode *talloc(void)
102104
{
103-
return (struct tnode *) malloc(sizeof(struct tnode));
105+
return malloc(sizeof(struct tnode));
104106
}
105107

106108
/*strDup: make a duplicate of s */
107109
char *strDup(char *s)
108110
{
109111
char *p;
110112

111-
p = (char *) malloc(strlen(s) + 1); /* +1 for '\0' */
113+
p = malloc(strlen(s) + 1); /* +1 for '\0' */
112114
if (p != NULL)
113115
strcpy(p, s);
114116
return p;
@@ -118,7 +120,7 @@ char *strDup(char *s)
118120
struct list *addlist(struct list *p, int ln)
119121
{
120122
if (p == NULL) {
121-
p = (struct list *) malloc(sizeof(struct list));
123+
p = malloc(sizeof(struct list));
122124
p->number = ln;
123125
p->next = NULL;
124126
} else if (p->number != ln) /* skip multi-occurrence on same line */
@@ -207,7 +209,7 @@ struct tnode *freetree(struct tnode *node)
207209
freetree(node->left);
208210
freetree(node->right);
209211
free(node->word);
210-
freelist(node->line); /* delete linked list nodes */
212+
freelist(node->line); /* delete linked list in nodes */
211213
free(node);
212214
}
213215
return node;
@@ -216,21 +218,18 @@ struct tnode *freetree(struct tnode *node)
216218
int main(void)
217219
{
218220
struct tnode *root; /* root node */
219-
struct key *p; /* currently searched word */
221+
struct key *sought; /* currently sought after word */
220222
char word[MAXWORD]; /* currently read word */
221-
int lineNumb; /* currently searched line */
222-
int *pLineNumb; /* pointer to change value of lineNumb */
223+
int lineNo = 1; /* currently searched line */
223224

224225
root = NULL;
225-
pLineNumb = &lineNumb;
226-
*pLineNumb = 1; /* start at line 1 */
227-
228-
while (getword(word, MAXWORD, pLineNumb) != EOF) {
229-
p = binsearch(word, noisetab, NKEYS); /* skip noise words */
230-
if (isalpha(word[0]) && p == NULL)
231-
root = addtree(root, word, lineNumb);
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);
232230
}
233231
treeprint(root);
234232
root = freetree(root); /* clean up */
233+
root = NULL;
235234
return 0;
236235
}

chapter06/6-4.c

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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 */
2324
int 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 */
2627
struct tnode *addtree(struct tnode *, char *);
2728
void treeprint(struct tnode *);
2829
struct tnode *copyTree(struct tnode *, struct tnode *);
2930
struct tnode *sortTree(struct tnode *, struct tnode *);
3031
struct 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

3637
int 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 */
4951
int getword(char *word, int lim)
5052
{
@@ -72,16 +74,16 @@ int getword(char *word, int lim)
7274
/* talloc: make a tnode */
7375
struct 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 */
7981
char *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 */
109111
void 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. */
119121
struct 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 */
135137
struct 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 */
146148
struct 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+
156159
int 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

Comments
 (0)