UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

7.9.8.  Inserting data and persisting a hash-table

Resources

  • Knowledge about dynamic data structures.

Work Plan

In SAUCEM Labs you are required to work in the design of an efficient application for students whose information is stored in a hash table. The information related to each student (stored in struct datos) is the name and the nia of the student. Internally, the hash table is implemented with an array of lists to different nodes of struct element_type type. Each of these nodes is part of a list and contains a pointer to the stored data, the key (clave), and a pointer to the next element of the list. The hash table information also includes the size of the array, and a pointer (**table) to the array of lists.

00: struct datos {
01:   	char *name;
02:   	int nia; 
03: };
04: struct elemento_t {
05:   	struct datos* valor;
06:     int clave;
07: 	struct elemento_t *next;
08: };
09: struct tabla_type {
10: 		int tamanho;
11: 		int num_elementos;
12: 		struct elemento_t **tabla;
13: };

The following figure shows an example of a hash table that stores the information related to three students called Eva (with nia: 33997), Paul (with nia: 33993) and Juan (with nia: 3399). The example assumes that the key (clave) for Eva and for Paul is 0 and for Juan is 32.

  1. Using this information you are required to implement the following function: void dump_hash_table_to_file(struct tabla type* htable, char* file_name). This function takes as an input a pointer to the hash table (htable) and the name of the file where the results are to be stored (file_name). By using fread the function is required to store all the elements (i.e. all nias and names) of the hash table into the file. If you cannot open the file, the function should return immediately without storing any data.

  2. By using the same information you are required to implement the following function in charge of inserting data within the hash table: void insert_element_into_hash_table(int clave, struct datos* valor, struct tabla_type *htable) This function is in charge of inserting a new element into the hash table pointed from htable. The function takes as an input the numerical key (clave) indicating in which position of the array is going to be inserted a new element. For this exercise, you may assume that clave is always less than the size of the table of pointers (you do not need to check it). The data for the student are passed in dynamic memory via a pointer called valor.