Part 1: get, put, del 4.1 Dictionary library (dict.c) In Part 1, you will have to implement the bulk of the dictionary library in dict.c. 4.1.1 Dictionary structure   You will start by giving a definition for your main structure struct dict. (You cannot change the name of this type, it is however aliased (typedef’d) in dict.h as dict_t to avoid having to type struct each time.) For instance, you may want to have a dictionary implemented as a linked list; in that case, you can store the head of that list and its length in your structure: struct dict { struct dict_list* head; size_t size; }; The first field is a pointer (*) to the head of the list, that is, its memory address. The struct dict_list itself contains the key, the value, and the address of the next element in the list: struct dict_list { char* key; char* val; struct dict_list* next; }; The end of the list is signaled by having next be NULL   // Forward declaration. void execute_command (const char* cmd); int main (int argc, char** argv) { ... } void execute_command (const char* cmd) { // Actual code } This is in fact what happens with C header files: they only contain the declarations of functions, but the code appears in other files. These forward declarations are further resolved during linking (Ch. 7).

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

 Part 1: get, put, del

4.1 Dictionary library (dict.c)

In Part 1, you will have to implement the bulk of the dictionary library in dict.c.

4.1.1 Dictionary structure

 

You will start by giving a definition for your main structure struct dict. (You cannot change the name of this type, it is however aliased (typedef’d) in dict.h as dict_t to avoid having to type struct each time.)

For instance, you may want to have a dictionary implemented as a linked list; in that case, you can store the head of that list and its length in your structure:

struct dict { struct dict_list* head; size_t size; };

The first field is a pointer (*) to the head of the list, that is, its memory address. The struct dict_list itself contains the key, the value, and the address of the next element in the list:

struct dict_list { char* key; char* val; struct dict_list* next; };

The end of the list is signaled by having next be NULL

 

// Forward declaration. void execute_command (const char* cmd); int main (int argc, char** argv) { ... } void execute_command (const char* cmd) { // Actual code }

This is in fact what happens with C header files: they only contain the declarations of functions, but the code appears in other files. These forward declarations are further resolved during linking (Ch. 7).

 

 

Note: I've already implemented dict_get and dict_create

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.

4.2 User interface (main.c)

The main function will contain the main loop of your program and should behave like this:

  1. Create an empty dictionary
  2. Print "> " on the standard output (this is important).
  3. Read the command from the user; the first three characters are the command name, then a space, then the arguments, and a newline character \n.
  4. If the command is unknown, exit.
  5. If end-of-file was read (EOF), destroy the dictionary and exit.
  6. Otherwise, execute the corresponding command and go back to 2.

To read from the standard input, you will be using the stream reading functions from the standard library, with stream being stdin, a variable defined in <stdio.h>:

  • int fgetc(FILE* stream): read one character of the given stream. This returns the constant EOF on end of file.
  • char* fgets (char* s, int size, FILE* stream): read in at most one less than size chars from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte (\0) is stored after the last character.

The function fgets is a bit tricky to work with when you want to read a whole line. Indeed, if the line is very long, fgets needs to be called multiple times. For this reason I will show you how to implement a robust “readline” function.

To write to the standard output, you will be using printf(3).

 

 

4.2.1 Commands

 

The three commands to be implemented in this part are:

  • get KEY: print the value associated with KEY followed by a newline \n. If no such value exists, a blank line is printed.
  • put KEY:VALUE: store the pair KEY / VALUE. Note that both KEY and VALUE can be arbitrary strings, with the only guarantee being that KEY does not contain a colon. Additionally, these strings don’t contain a newline \n. Nothing is printed in return.
  • del KEY: delete the pair associated with KEY, if it exists. Nothing is printed in return, even if the key does not exist in the dictionary.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Concept of memory addresses in pointers
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY