4.1.4 dict_put It is now time for: void dict_put (dict_t* dict, const char* key, const char* val); This function stores the pair key/val in the dictionary dict, possibly erasing the previous value associated with key. It should make private copies of key and val as required. This means that the function cannot use, for instance, my_element->val = val. To make a copy of a string s, one could: 1. allocate strlen(s) + 1 bytes using malloc(3) (strlen(3) returns the length of a string, not counting the end-of-string marker) 2. go through the strlen(s) + 1 bytes of s and copy each byte, using either a loop or the function memcpy(3) Again, the standard library provides a function for this: strdup (3)-remember that since its return value was malloc'd, it needs to be free'd. The function works as follows: •If key is found in dict, then the associated value should be freed and replaced by a copy of val. If key is not found in dict, then a new element is added to the list; that element's key is a copy of key and its value is a copy of val. 4.1.5 dict_del Finally, you should implement: void dict_del (dict_t* dict, const char* key); This function goes through dict searching for key, if it finds it, it should delete the corresponding key/value pair. The memory allocated for the element and its key/value pair should be free'd. If the key does not exist, this function does nothing.

icon
Related questions
Question
4.1.4 dict_put
It is now time for:
void
dict_put (dict_t* dict, const char* key, const char* val);
This function stores the pair key/val in the dictionary dict, possibly erasing the previous value associated with key.
It should make private copies of key and val as required. This means that the function cannot use, for instance, my_element->val = val. To make a copy of a string s, one
could:
1. allocate strlen(s) + 1 bytes using malloc(3) (strlen(3) returns the length of a string, not counting the end-of-string marker)
2. go through the strlen(s) + 1 bytes of s and copy each byte, using either a loop or the function memcpy(3)
Again, the standard library provides a function for this: strdup (3)-remember that since its return value was malloc'd, it needs to be free'd.
The function works as follows:
•If key is found in dict, then the associated value should be freed and replaced by a copy of val.
If key is not found in dict, then a new element is added to the list; that element's key is a copy of key and its value is a copy of val.
4.1.5 dict_del
Finally, you should implement:
void
dict_del (dict_t* dict, const char* key);
This function goes through dict searching for key, if it finds it, it should delete the corresponding key/value pair. The memory allocated for the element and its key/value pair
should be free'd. If the key does not exist, this function does nothing.
Transcribed Image Text:4.1.4 dict_put It is now time for: void dict_put (dict_t* dict, const char* key, const char* val); This function stores the pair key/val in the dictionary dict, possibly erasing the previous value associated with key. It should make private copies of key and val as required. This means that the function cannot use, for instance, my_element->val = val. To make a copy of a string s, one could: 1. allocate strlen(s) + 1 bytes using malloc(3) (strlen(3) returns the length of a string, not counting the end-of-string marker) 2. go through the strlen(s) + 1 bytes of s and copy each byte, using either a loop or the function memcpy(3) Again, the standard library provides a function for this: strdup (3)-remember that since its return value was malloc'd, it needs to be free'd. The function works as follows: •If key is found in dict, then the associated value should be freed and replaced by a copy of val. If key is not found in dict, then a new element is added to the list; that element's key is a copy of key and its value is a copy of val. 4.1.5 dict_del Finally, you should implement: void dict_del (dict_t* dict, const char* key); This function goes through dict searching for key, if it finds it, it should delete the corresponding key/value pair. The memory allocated for the element and its key/value pair should be free'd. If the key does not exist, this function does nothing.
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer