@@ -17,7 +17,7 @@ typedef struct hashTable_t {
17
17
* linked list */
18
18
} hashTable ;
19
19
20
- hashTable * ht_create (unsigned int size ) {
20
+ static hashTable * ht_create (unsigned int size ) {
21
21
hashTable * ht ;
22
22
if (size < 1 )
23
23
return NULL ;
@@ -39,7 +39,7 @@ hashTable *ht_create(unsigned int size) {
39
39
}
40
40
41
41
/* free the items in a hashTable. free each individual row and column */
42
- void ht_free (hashTable * ht ) {
42
+ static void ht_free (hashTable * ht ) {
43
43
list * tmp ;
44
44
45
45
if (!ht )
@@ -62,7 +62,7 @@ void ht_free(hashTable *ht) {
62
62
free (ht );
63
63
}
64
64
65
- unsigned int hash (const char * key , unsigned int size ) {
65
+ static unsigned int hash (const char * key , unsigned int size ) {
66
66
unsigned int hash = 0 , i = 0 ;
67
67
68
68
while (key && key [i ]) {
@@ -72,7 +72,36 @@ unsigned int hash(const char *key, unsigned int size) {
72
72
return hash ;
73
73
}
74
74
75
- int ht_put (hashTable * ht , const char * key , const char * value ) {
75
+ static void node_handle (hashTable * ht , list * node ) {
76
+ unsigned int i = hash (node -> key , ht -> size );
77
+ list * tmp = ht -> array [i ];
78
+
79
+ if (ht -> array [i ] != NULL ) {
80
+ tmp = ht -> array [i ];
81
+ while (tmp != NULL ) {
82
+ if (strcmp (tmp -> key , node -> key ) == 0 )
83
+ break ;
84
+ tmp = tmp -> next ;
85
+ }
86
+ if (tmp == NULL ) {
87
+ node -> next = ht -> array [i ];
88
+ ht -> array [i ] = node ;
89
+ }
90
+ else {
91
+ free (tmp -> value );
92
+ tmp -> value = strdup (node -> value );
93
+ free (node -> value );
94
+ free (node -> key );
95
+ free (node );
96
+ }
97
+ }
98
+ else {
99
+ node -> next = NULL ;
100
+ ht -> array [i ] = node ;
101
+ }
102
+ }
103
+
104
+ static int ht_put (hashTable * ht , const char * key , const char * value ) {
76
105
list * node ;
77
106
78
107
if (!ht ) {
@@ -92,7 +121,7 @@ int ht_put(hashTable *ht, const char *key, const char *value) {
92
121
return 0 ;
93
122
}
94
123
95
- char * ht_get (hashTable * ht , const char * key ) {
124
+ static char * ht_get (hashTable * ht , const char * key ) {
96
125
char * key_cp ;
97
126
unsigned int i ;
98
127
list * tmp ;
@@ -118,35 +147,6 @@ char *ht_get(hashTable *ht, const char *key) {
118
147
return tmp -> value ;
119
148
}
120
149
121
- void node_handle (hashTable * ht , list * node ) {
122
- unsigned int i = hash (node -> key , ht -> size );
123
- list * tmp = ht -> array [i ];
124
-
125
- if (ht -> array [i ] != NULL ) {
126
- tmp = ht -> array [i ];
127
- while (tmp != NULL ) {
128
- if (strcmp (tmp -> key , node -> key ) == 0 )
129
- break ;
130
- tmp = tmp -> next ;
131
- }
132
- if (tmp == NULL ) {
133
- node -> next = ht -> array [i ];
134
- ht -> array [i ] = node ;
135
- }
136
- else {
137
- free (tmp -> value );
138
- tmp -> value = strdup (node -> value );
139
- free (node -> value );
140
- free (node -> key );
141
- free (node );
142
- }
143
- }
144
- else {
145
- node -> next = NULL ;
146
- ht -> array [i ] = node ;
147
- }
148
- }
149
-
150
150
int main (void ) {
151
151
hashTable * ht ;
152
152
if (!(ht = ht_create (1 ))) {
0 commit comments