Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 1. You will be implementing a dictionary structure (storing pairs key/value), and reading the user input to manipulate the structure. The commands implemented in

  • Part 1. You will be implementing a dictionary structure (storing pairs key/value), and reading the user input to manipulate the structure. The commands implemented in that stage are put, get, and del. It will be important not to leak any memory in these functions.
  • Part 2. You will add two more commands: clr, which clears the dictionary, and siz, which prints the size of the dictionary. Again, clr will have to make sure not to leak any memory. You will also implement a destructor for your dictionary object.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
// Create a dictionary. dict_t* dict_create (); // Free a dictionary. void dict_destroy (dict_t* dic); // Put an element in a dictionary. key is case sensitive, val is a string. If // the key already exists, its value is updated. If val is NULL, the pair is // deleted. void dict_put (dict_t* dic, const char* key, const char* val); // Return the value associated with key, or NULL if none. char* dict_get (const dict_t* dic, const char* key); // Delete the pair associated with key. void dict_del (dict_t* dic, const char* key); // Return the size of the dict. size_t dict_size (const dict_t* dic); // Delete all elements in a dictionary. void dict_clear (dict_t* dic); // Apply fun to each pair key/val; arg is an extra argument passed to fun. void dict_apply (const dict_t* dic, const dict_apply_fun_t fun, void* arg);[main . c: In function 'dict_put' : main. c:29:8: error: 'dict_list_t' {aka 'struct dict_list'} has no member named 'value'; did you mean 'val' ? d->value = value; ANNAN val main. c:29:16: error: 'value' undeclared (first use in this function); did you mean 'val'? d->value = value; ANNAN val main. c:29:16: note: each undeclared identifier is reported only once for each function it appears in main. c:30:13: error: incompatible types when assigning to type 'struct dict_list *' from type 'dict_t' {aka 'struct dict'} d->next = *dict; main. c:31:11: error: incompatible types when assigning to type 'dict_t' {aka 'struct dict') from type 'dict_list_t *' {aka 'struct dict_list *'} *dict = d; main. c: In function 'dict_del' : main . c : 40:44: : error: 'dict_t' {aka 'struct dict'} has no member named 'next' for (ptr = dict; ptr != NULL; ptr = ptr->next) { main. c:41:23: error: 'dict_t' {aka 'struct dict'} has no member named 'key' if (stromp(ptr->key, key) == 0) main. c:42:23: error: 'dict_t' {aka 'struct dict'} has no member named 'value' return ptr->value; }}return NULL; main . c:42:20: error: 'return' with a value, in function returning void [-Werror] return ptr->value; }}return NULL; main . c:38:6: note: declared here void dict_del (dict_t* dict, const char* key) { ANNNNNNN main . c: 42:40: error: 'return' with a value, in function returning void [-Werror] return ptr->value; }}return NULL; ANNN main . c:38:6: note: declared here void dict_del (dict_t* dict, const char* key) { ANNNNNNN main. c: In function 'dict_size' : main . c: 48:9: error: expected expression before '->' token return->size; main . c: In function 'dict_clear' : main. c:52:3: error: unknown type name 'delete'; did you mean 'dev_t'? delete dict; dev_t main. c:52:10: error: 'dict' redeclared as different kind of symbol delete dict; main. c:51:26: note: previous definition of 'dict' was here void dict_clear (dict_t* dict) { NNNNNNNNANNN main . c:52:10: error: unused variable 'dict' [-Werror=unused-variable] delete dict; main. c: At top level: main. c:61:1: error: expected identifier or '(' before numeric constant 1, 1 main . c: In function 'dict_size' : main . c:49:1: error: control reaches end of non-void function [-Werror=return-type]#include #include #include "dict.h" typedef struct dict_list { char* key; char* val; struct dict_list* next; } dict_list_t; typedef struct dict { dict_list_t* head; size_t size; } dict_t; dict_t* dict_create () { return malloc (sizeof (dict_t) ) ; void dict_put (dict_t* dict, const char* key, const char* val) { dict_list_t *d = malloc(sizeof(struct dict_list) ) ; d->key = malloc (strlen(key)+1) ; strcpy (d->key, key) ; d->value = value; d->next = *dict; *dict = d; char* dict_get (const dict_t* dict, const char* key) { return NULL; void dict_del (dict_t* dict, const char* key) { dict_t *ptr; for (ptr = dict; ptr != NULL; ptr = ptr->next) { if (stromp (ptr->key, key) == 0) { return ptr->value; }}return NULL; size_t dict_size (const dict_t* dict) { return->size; void dict_clear (dict_t* dict) { delete dict; void dict_destroy (dict_t* dict) { free (dict) ;\f

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions