Skip to content

Commit 3625330

Browse files
committed
move printing code from main to printtab
1 parent 2221d2e commit 3625330

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

chapter06/6-5.c

+13-9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ char *strDup(char *);
2222
struct nlist *lookup(char *);
2323
struct nlist *install(char *, char *);
2424
void undef(char *);
25+
void printtab(struct nlist *[], int);
2526
void freetable(struct nlist *[], int);
2627

2728
/* globals */
@@ -95,6 +96,16 @@ void undef(char *s)
9596
}
9697
}
9798

99+
/* printtab: prints content of hashtable, linear scan */
100+
void printtab(struct nlist *node[], int size)
101+
{
102+
int i;
103+
for (i = 0; i < size; i++)
104+
if (node[i] != NULL)
105+
printf("%i name: %s defn: %s\n",
106+
i, node[i]->name, node[i]->defn);
107+
}
108+
98109
/* freetable: free table's (and its content's) allocated memory from heap */
99110
void freetable(struct nlist *node[], int size)
100111
{
@@ -111,27 +122,20 @@ void freetable(struct nlist *node[], int size)
111122
int main(void)
112123
{
113124
struct nlist *p;
114-
int i;
115125

116126
/* insert nodes (skipped error checking) */
117127
p = install("YES", "1");
118128
p = install("NO", "0");
119129

120130
printf("Hash Table Values:\n");
121-
for (i = 0; i < HASHSIZE; i++)
122-
if (hashtab[i] != NULL)
123-
printf("%i name: %s defn: %s\n",
124-
i, hashtab[i]->name, hashtab[i]->defn);
131+
printtab(hashtab, HASHSIZE);
125132

126133
/* delete a node */
127134
printf("\nDelete \"YES\"\n\n");
128135
undef("YES");
129136

130137
printf("Hash Table Values (After Deletion):\n");
131-
for (i = 0; i < HASHSIZE; i++)
132-
if (hashtab[i] != NULL)
133-
printf("%i name: %s defn: %s\n",
134-
i, hashtab[i]->name, hashtab[i]->defn);
138+
printtab(hashtab, HASHSIZE);
135139

136140
freetable(hashtab, HASHSIZE); /* clean up */
137141
return 0;

chapter06/6-6.c

+13-7
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ char *strDup(char *);
4949
unsigned hash(char *);
5050
struct nlist *lookup(char *);
5151
struct nlist *install(char *, char *);
52+
void printtab(struct nlist *[], int);
5253
void freetable(struct nlist *[], int);
5354

5455
/* binsearch: find word in tab[0]...tab[n - 1] */
@@ -218,6 +219,16 @@ struct nlist *install(char *name, char *defn)
218219
return NULL;
219220
return np;
220221
}
222+
/* printtab: prints content of hashtable, linear scan */
223+
void printtab(struct nlist *node[], int size)
224+
{
225+
int i;
226+
227+
for (i = 0; i < size; i++)
228+
if (node[i] != NULL)
229+
printf("%i name: %s defn: %s\n",
230+
i, node[i]->name, node[i]->defn);
231+
}
221232

222233
/* freetable: free table's (and its content's) allocated memory from heap */
223234
void freetable(struct nlist *node[], int size)
@@ -239,7 +250,7 @@ int main (void)
239250
char word[MAXWORD];
240251
char defn[MAXLEN];
241252
char *name, *keyword = "#define";
242-
int ctrline, len, i;
253+
int ctrline, len;
243254

244255
name = word; /* unnecessary. Added for clarity */
245256

@@ -253,13 +264,8 @@ int main (void)
253264
install(name, defn);
254265
ctrline = 0;
255266
}
256-
257-
/* print table */
258267
printf("Hash Table Values:\n");
259-
for (i = 0; i < HASHSIZE; i++)
260-
if (hashtab[i] != NULL)
261-
printf("%i name: %s defn: %s\n",
262-
i, hashtab[i]->name, hashtab[i]->defn);
268+
printtab(hashtab, HASHSIZE);
263269
freetable(hashtab, HASHSIZE); /* clean up */
264270
return 0;
265271
}

0 commit comments

Comments
 (0)