#include #include #include #define capacity 101 // Size of Hash Table int hash_function(char* str) {     int i = 0;     for (int j=0; str[j]; j++)         i += str[j];     return i % capacity; } struct Ht_item {     char* name;     int numberCourses; }; // Hash Table Definition struct HashTable {     struct Ht_item** items;     int size;     int count; }; struct Ht_item* create_item(char* key, int value) {          struct Ht_item* item = (struct Ht_item*) malloc (sizeof(struct Ht_item));     item->name = (char*) malloc (strlen(key) + 1);     item->numberCourses = value;     strcpy(item->name, key);     item->numberCourses = value;     return item; } struct HashTable* create_table(int size) {     // Creates a New Hash Table     struct HashTable* table = (struct HashTable*) malloc (sizeof(struct HashTable));     table->size = size;     table->count = 0;     table->items = (struct Ht_item**) calloc (table->size, sizeof(struct Ht_item*));     for (int i=0; isize; i++)         table->items[i] = 0;     return table; } void put(struct HashTable* table, char* key, int value) {     struct Ht_item* item = create_item(key, value);     int index = hash_function(key);     struct Ht_item* current_item = table->items[index];     if (current_item == 0) {         // Key Not Available         if (table->count == table->size) {             // Hash Table Full             printf("Hash Table Full...\n");             return;         }                 // Direct Insertion         table->items[index] = item;         table->count++;     }     else {         if (strcmp(current_item->name, key) == 0) {             table->items[index]->numberCourses = value;             return;         }         else {             // Write the collision case using loop?                                  }     } } int contains(struct HashTable* table, char* key) {            // Search in Hash Table     // Return 0 if Not Available     int index = hash_function(key);     struct Ht_item* item = table->items[index];     if (item != 0) {         if (strcmp(item->name, key) == 0)             return item->numberCourses;     }     return 0; }

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define capacity 101 // Size of Hash Table

int hash_function(char* str) {
    int i = 0;
    for (int j=0; str[j]; j++)
        i += str[j];
    return i % capacity;
}

struct Ht_item {
    char* name;
    int numberCourses;
};

// Hash Table Definition
struct HashTable {
    struct Ht_item** items;
    int size;
    int count;
};

struct Ht_item* create_item(char* key, int value) {
    
    struct Ht_item* item = (struct Ht_item*) malloc (sizeof(struct Ht_item));
    item->name = (char*) malloc (strlen(key) + 1);
    item->numberCourses = value;

    strcpy(item->name, key);
    item->numberCourses = value;

    return item;
}

struct HashTable* create_table(int size) {
    // Creates a New Hash Table
    struct HashTable* table = (struct HashTable*) malloc (sizeof(struct HashTable));
    table->size = size;
    table->count = 0;
    table->items = (struct Ht_item**) calloc (table->size, sizeof(struct Ht_item*));
    for (int i=0; i<table->size; i++)
        table->items[i] = 0;

    return table;
}

void put(struct HashTable* table, char* key, int value) {
    struct Ht_item* item = create_item(key, value);
    int index = hash_function(key);
    struct Ht_item* current_item = table->items[index];

    if (current_item == 0) {
        // Key Not Available
        if (table->count == table->size) {
            // Hash Table Full
            printf("Hash Table Full...\n");
            return;
        }

       
        // Direct Insertion
        table->items[index] = item;
        table->count++;
    }

    else {
        if (strcmp(current_item->name, key) == 0) {
            table->items[index]->numberCourses = value;
            return;
        }

        else {
            // Write the collision case using loop?
            
           
        }
    }
}

int contains(struct HashTable* table, char* key) {       
    // Search in Hash Table
    // Return 0 if Not Available
    int index = hash_function(key);
    struct Ht_item* item = table->items[index];
    if (item != 0) {
        if (strcmp(item->name, key) == 0)
            return item->numberCourses;
    }
    return 0;
}

int get(struct HashTable* table, char* key){

}

void display(struct HashTable* table, char* key) {
    int val;
    if ((val = contains(table, key)) == 0) {
        printf("Key: %s Not Available...\n", key);
        return;
    }
    else {
        printf("Key: %s, Value: %d\n", key, val);
    }
}


int main() {
    
    struct HashTable* ht = create_table(capacity);
    
    put(ht, "Alex", 4);
    put(ht, "Amy", 3);
    put(ht, "Tiny", 7);
    put(ht, "Amy", 2);
    put(ht, "Cary", 3);
    
    display(ht, "Mark");
    display(ht, "Amy");
    display(ht, "Tiny");
    display(ht, "Cary");
    display(ht, "Alex");
    return 0;
}

The code for the problem is given above.

// Write the collision case using loop? (handle the collision case in the comment section and it will search linearly.)

The get function itself gives the number of lessons learned for the most sent key. send it. If the key is not present in the map, it returns -1. (Write the get function in accordance with the definition.)

Complete the code by adding the specified problems to the code.

 

 

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
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