Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include struct Node { int val; struct Node * next; }; struct HashTable { int size; struct Node ** table; }; struct Node *

image text in transcribedimage text in transcribed

#include  #include  struct Node { int val; struct Node * next; }; struct HashTable { int size; struct Node ** table; }; struct Node * create_node(int val); struct HashTable * create_table(int size); void destroy_table(struct HashTable * h); void display_table(struct HashTable *h); int hash_function(int key, int size); void insert(struct HashTable *h, int val); int search(struct HashTable * h, int val); struct Node * delete(struct HashTable * h, int val); void main() { struct HashTable * h = create_table(8); display_table(h); int val_insert[12] = {12, 31, 45, 83, 72, 29, 84, 39, 61, 24, 10, 50}; for (int i=0; isize); struct Node *p = h->table[i]; h->table[i] = create_node(val); h->table[i]->next = p; } struct Node * create_node(int val) { struct Node * p= (struct Node *) malloc(sizeof(struct Node)); p->val = val; p->next = NULL; return p; } int hash_function(int val, int size) { return (97*val + 13)%size; } void display_table(struct HashTable *h) { printf(" Hash Table "); for (int i=0; isize; i++) { printf("%2d: ",i); for (struct Node *p=h->table[i]; p!=NULL; p=p->next) { printf(" ->%3d ",p->val); } printf(" -> / "); } printf(" "); } void destroy_table(struct HashTable * h) { for (int i=0; isize; i++) { for (struct Node * p=h->table[i]; p!=NULL; ) { struct Node * temp=p; p=p->next; free(temp); } } free(h); } struct HashTable * create_table(int size) { struct HashTable * h = (struct HashTable *) malloc(sizeof(struct HashTable)); h->size = size; h->table = (struct Node **) malloc(sizeof(struct Node *)*size); for (int i=0; itable[i] = NULL; } return h; } 
Problem 2 [ 2 pt] Hash table implementation. Attached is a program hash.c that has a hash table. The operations of search and delete are not implemented properly: - int search(struct HashTable h, int val); - struct Node * delete(struct HashTable * h, int val); Implement search and delete properly The output should look like this: Hash Table 0: / 1. / 2: / 3: / 4. >/ 5: / 6: >/ 7:7/ Hash Table 0:>83 1: 8412/ 2: 612945/ 3: >/ 4. 3931/ 5:24721 6: / 7:5010/ Search value 29: found Search value 99: not found Search value 72: found Search value 20 : not found Hash Table 0:83/ 1:8412/ 2: 612945/ 3: / 4: 3931/ 5:2472/ 6: / 7: 5010/ Delete value 29: found Delete value 99: not found Delete value 72: found Delete value 20: not found Delete value 24: found Delete value a3: found Delete value 45 : found Hash Table D: / 1: 8412/ 2: 61/ 3: / 4. 3931/ 5: / 6: / 7:5010/

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

More Books

Students also viewed these Databases questions