11/*
22 * Exercise 6-5. Write a function undef that will remove a name and a
33 * definition from the table maintained by lookup and install.
4+ *
45 * By Faisal Saadatmand
56 */
67
@@ -18,7 +19,7 @@ struct nlist { /* table entry: */
1819
1920/* functions */
2021unsigned hash (char * );
21- char * strDup (char * );
22+ // char *strDup(char *);
2223struct nlist * lookup (char * );
2324struct nlist * install (char * , char * );
2425void undef (char * );
@@ -33,8 +34,8 @@ char *strDup(char *s)
3334{
3435 char * p ;
3536
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 )
3839 strcpy (p , s );
3940 return p ;
4041}
@@ -44,7 +45,7 @@ unsigned hash(char *s)
4445{
4546 unsigned hashval ;
4647
47- for (hashval = 0 ; * s != '\0' ; s ++ )
48+ for (hashval = 0 ; * s ; s ++ )
4849 hashval = * s + (31 * hashval );
4950 return hashval % HASHSIZE ;
5051}
@@ -54,8 +55,8 @@ struct nlist *lookup(char *s)
5455{
5556 struct nlist * np ;
5657
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 ))
5960 return np ; /* found */
6061 return NULL ;
6162}
@@ -66,9 +67,9 @@ struct nlist *install(char *name, char *defn)
6667 struct nlist * np ;
6768 unsigned hashval ;
6869
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 )))
7273 return NULL ; /* no (heap) memory */
7374 hashval = hash (name );
7475 np -> next = hashtab [hashval ];
@@ -78,7 +79,7 @@ struct nlist *install(char *name, char *defn)
7879
7980 np -> defn = strDup (defn ); /* copy definition */
8081
81- if (np -> defn == NULL )
82+ if (! np -> defn )
8283 return NULL ;
8384 return np ;
8485}
@@ -88,7 +89,7 @@ void undef(char *s)
8889{
8990 struct nlist * np ;
9091
91- if ((np = lookup (s )) != NULL ) {
92+ if ((np = lookup (s ))) {
9293 free ((void * ) np -> name ); /* free name memory */
9394 free ((void * ) np -> defn ); /* free definition memory */
9495 hashtab [hash (s )] = np -> next ; /* clear index or relink next node */
@@ -100,19 +101,18 @@ void undef(char *s)
100101void printtab (struct nlist * node [], int size )
101102{
102103 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 );
107107}
108108
109109/* freetable: free table's (and its content's) allocated memory from heap */
110110void freetable (struct nlist * node [], int size )
111111{
112112 int i ;
113113
114- for (i = 0 ; i < size ; i ++ )
115- if (node [i ] != NULL ) {
114+ for (i = 0 ; i < size ; ++ i )
115+ if (node [i ]) {
116116 free (node [i ]-> name );
117117 free (node [i ]-> defn );
118118 free (node [i ]);
@@ -126,17 +126,13 @@ int main(void)
126126 /* insert nodes (skipped error checking) */
127127 p = install ("YES" , "1" );
128128 p = install ("NO" , "0" );
129-
130129 printf ("Hash Table Values:\n" );
131130 printtab (hashtab , HASHSIZE );
132-
133131 /* delete a node */
134132 printf ("\nDelete \"YES\"\n\n" );
135133 undef ("YES" );
136-
137134 printf ("Hash Table Values (After Deletion):\n" );
138135 printtab (hashtab , HASHSIZE );
139-
140136 freetable (hashtab , HASHSIZE ); /* clean up */
141137 return 0 ;
142138}
0 commit comments