| |

Question: #include #include #include #define TABLE_SIZE 256 typedef stru – Free Chegg Question Answer

#include 
#include 
#include 

#define TABLE_SIZE 256


typedef struct {
    char *name;   // variable name
    char *type;   // variable type
    int var_size; // variable size
} RECORD;

RECORD symtab[TABLE_SIZE];

// ADD YOUR CODE HERE



int main(int argc, char **argv)
{
    init_hash_table();

    hash_insert("num1", "REAL");
    hash_insert("num2", "INTEGER");
    hash_insert("num3", "INTEGER");
    hash_insert("num4", "REAL");

    printf("Insert Done ...\n");
    char *str1 = "num1";
    char *str2 = "num3";
    char *str3 = "Num1";

    if (hash_search(str1) != -1)
        printf("Element %s found\n",str1);
    else
        printf("Element %s not found\n",str1);

   if (hash_search(str2) != -1)
        printf("Element %s found\n",str2);
    else
        printf("Element %s not found\n",str2);

    if (hash_search(str3) != -1)
        printf("Element %s found\n",str3);
    else
        printf("Element %s not found\n",str3);

    free_hash_table();

    return 0;
}

ADDITIONAL INFORMATION:

In this assignment, you are going to write a C program to store and search items using a hash table that includes the following functions

• init_hash_table // initialize a hash table

• free_hash_table // release a hash table

• hash_insert // insert an element to a hash table

• hash_search // search an element from a hash table

• hash // a hash function

You may use the following hash function

Hash(k, i) = (i*i + i) % TABLESIZE;

The following are the pseudocode where T is the hash table, k is the item to be inserted, and m is the table size.

HASH-INSERT(T, k)

1 i=0

repeat

3 j=h(k,i)

if T[j]==NIL

5 T[j]=K

return j

else i=i+1

until i==m

error “hash table overflow”

HASH-SEARCH(T,k)

1 i=0

repeat

3 j=h(k,i)

if T[j]==k

return j

6 i-i+1

until T[j]==NIL or i==m

return NIL

Expected Results:

Insert Done …

Element num1 found

Element num3 found

Element Num1 not found

Transcribed text From Image: 

Expert Chegg Question Answer:

free chegg question answer
Smart Teacher From Answerie.com
Answer:

Answer


Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TABLE_SIZE 256

typedef struct {
    char *name;    // variable name
    char *type;    // variable type
    int var_size;  // variable size
} RECORD;

RECORD symtab[TABLE_SIZE];

/**
 * Function to initialize symtab
 **/
void init_hash_table() {
    for (int i = 0; i < TABLE_SIZE; i++) {
        symtab[i].name = NULL;
        symtab[i].type = NULL;
        symtab[i].var_size = 0;
    }
}

/**
 * Function to free memory allocated by symtab
 **/
void free_hash_table() {
    for (int i = 0; i < TABLE_SIZE; i++) {
        free(symtab[i].name);
        free(symtab[i].type);
    }
}

/**
 * Function to find the hash of a string
 * @param str const char *
 * @param i int
 * @return int
 **/
int hash(const char *str, int i) {
    int len = strlen(str);
    int k = 0;
    for (int i = 0; i < len; i++) {
        k = (k + str[i]) % TABLE_SIZE;
    }
    return (k + i * i) % TABLE_SIZE;
}

/**
 * Function to insert element into symtab
 * @param name const char *
 * @param type const char *
 * @return int
 **/
int hash_insert(const char *name, const char *type) {
    int h;
    for (int i = 0; i < TABLE_SIZE; i++) {
        h = hash(name, i);
        if (symtab[h].name == NULL) {
            // allocate memory for name
            symtab[h].name = (char *)malloc(sizeof(char) * (strlen(name) + 1));

            // allocate memory for type
            symtab[h].type = (char *)malloc(sizeof(char) * (strlen(type) + 1));

            strcpy(symtab[h].name, name);  // copy name to table
            strcpy(symtab[h].type, type);  // copy type to teble
            return h;
        }
    }
    printf("Hash table overflow!!");
    exit(1);
}

/**
 * Function to search a name in symtab
 * @param name const char *
 * @return int
 *  returns the index of name in symtab if found else -1
 **/
int hash_search(const char *name) {
    int h;
    for (int i = 0; i < TABLE_SIZE; i++) {
        h = hash(name, i);
        if (symtab[h].name == NULL) {
            break;
        } else if (strcmp(symtab[h].name, name) == 0) {
            return h;
        }
    }
    return -1;
}

int main(int argc, char **argv) {
    init_hash_table();

    hash_insert("num1", "REAL");
    hash_insert("num2", "INTEGER");
    hash_insert("num3", "INTEGER");
    hash_insert("num4", "REAL");

    printf("Insert Done ...\n");
    char *str1 = "num1";
    char *str2 = "num3";
    char *str3 = "Num1";

    if (hash_search(str1) != -1)
        printf("Element %s found\n", str1);
    else
        printf("Element %s not found\n", str1);

    if (hash_search(str2) != -1)
        printf("Element %s found\n", str2);
    else
        printf("Element %s not found\n", str2);

    if (hash_search(str3) != -1)
        printf("Element %s found\n", str3);
    else
        printf("Element %s not found\n", str3);

    free_hash_table();

    return 0;
}

Screenshots:

<img src="https://media.cheggcdn.com/coop/8e2/8e279750-4b60-471f-b122-0c4d103b1556/1631453755708_code_snapshot.png" alt="hash_table.c #include <stdio.h> 2 #include <stdlib.h> #include
output Insert Done ... Element numi found Element num3 found Element Num1 not found

Free Chegg Question Answer

Leave a Reply

Your email address will not be published. Required fields are marked *