1
1
/*
2
2
* Exercise 6-5. Write a function undef that will remove a name and a
3
3
* definition from the table maintained by lookup and install.
4
+ *
4
5
* By Faisal Saadatmand
5
6
*/
6
7
@@ -18,7 +19,7 @@ struct nlist { /* table entry: */
18
19
19
20
/* functions */
20
21
unsigned hash (char * );
21
- char * strDup (char * );
22
+ // char *strDup(char *);
22
23
struct nlist * lookup (char * );
23
24
struct nlist * install (char * , char * );
24
25
void undef (char * );
@@ -33,8 +34,8 @@ char *strDup(char *s)
33
34
{
34
35
char * p ;
35
36
36
- p = ( char * ) malloc (strlen (s ) + 1 ); /* +1 for '\0' */
37
- if (p != NULL )
37
+ p = malloc (strlen (s ) + 1 ); /* +1 for '\0' */
38
+ if (p )
38
39
strcpy (p , s );
39
40
return p ;
40
41
}
@@ -44,7 +45,7 @@ unsigned hash(char *s)
44
45
{
45
46
unsigned hashval ;
46
47
47
- for (hashval = 0 ; * s != '\0' ; s ++ )
48
+ for (hashval = 0 ; * s ; s ++ )
48
49
hashval = * s + (31 * hashval );
49
50
return hashval % HASHSIZE ;
50
51
}
@@ -54,8 +55,8 @@ struct nlist *lookup(char *s)
54
55
{
55
56
struct nlist * np ;
56
57
57
- for (np = hashtab [hash (s )]; np != NULL ; np = np -> next )
58
- if (strcmp (s , np -> name ) == 0 )
58
+ for (np = hashtab [hash (s )]; np ; np = np -> next )
59
+ if (! strcmp (s , np -> name ))
59
60
return np ; /* found */
60
61
return NULL ;
61
62
}
@@ -66,9 +67,9 @@ struct nlist *install(char *name, char *defn)
66
67
struct nlist * np ;
67
68
unsigned hashval ;
68
69
69
- if ((np = lookup (name )) == NULL ) { /* not found */
70
- np = ( struct nlist * ) malloc (sizeof (* np ));
71
- if (np == NULL || (np -> name = strDup (name )) == NULL )
70
+ if (! (np = lookup (name ))) { /* not found */
71
+ np = malloc (sizeof (* np ));
72
+ if (! np || ! (np -> name = strDup (name )))
72
73
return NULL ; /* no (heap) memory */
73
74
hashval = hash (name );
74
75
np -> next = hashtab [hashval ];
@@ -78,7 +79,7 @@ struct nlist *install(char *name, char *defn)
78
79
79
80
np -> defn = strDup (defn ); /* copy definition */
80
81
81
- if (np -> defn == NULL )
82
+ if (! np -> defn )
82
83
return NULL ;
83
84
return np ;
84
85
}
@@ -88,7 +89,7 @@ void undef(char *s)
88
89
{
89
90
struct nlist * np ;
90
91
91
- if ((np = lookup (s )) != NULL ) {
92
+ if ((np = lookup (s ))) {
92
93
free ((void * ) np -> name ); /* free name memory */
93
94
free ((void * ) np -> defn ); /* free definition memory */
94
95
hashtab [hash (s )] = np -> next ; /* clear index or relink next node */
@@ -100,19 +101,18 @@ void undef(char *s)
100
101
void printtab (struct nlist * node [], int size )
101
102
{
102
103
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 );
104
+ for (i = 0 ; i < size ; ++ i )
105
+ if (node [i ])
106
+ printf ("%i name: %s defn: %s\n" , i , node [i ]-> name , node [i ]-> defn );
107
107
}
108
108
109
109
/* freetable: free table's (and its content's) allocated memory from heap */
110
110
void freetable (struct nlist * node [], int size )
111
111
{
112
112
int i ;
113
113
114
- for (i = 0 ; i < size ; i ++ )
115
- if (node [i ] != NULL ) {
114
+ for (i = 0 ; i < size ; ++ i )
115
+ if (node [i ]) {
116
116
free (node [i ]-> name );
117
117
free (node [i ]-> defn );
118
118
free (node [i ]);
@@ -126,17 +126,13 @@ int main(void)
126
126
/* insert nodes (skipped error checking) */
127
127
p = install ("YES" , "1" );
128
128
p = install ("NO" , "0" );
129
-
130
129
printf ("Hash Table Values:\n" );
131
130
printtab (hashtab , HASHSIZE );
132
-
133
131
/* delete a node */
134
132
printf ("\nDelete \"YES\"\n\n" );
135
133
undef ("YES" );
136
-
137
134
printf ("Hash Table Values (After Deletion):\n" );
138
135
printtab (hashtab , HASHSIZE );
139
-
140
136
freetable (hashtab , HASHSIZE ); /* clean up */
141
137
return 0 ;
142
138
}
0 commit comments