Question: Help needed! I wrote a implement for hash table, but I have some problems: 1. Please find a way to replace the global value int
Help needed! I wrote a implement for hash table, but I have some problems:
1. Please find a way to replace the global value int size and int max.
2. Write a function to print probe value for each insert, and give the average probe value for all insert.
3. I try to input a big txt file to this implement, but the void init_array() always crash when I change max=10 to max=7477, I don't know why.
The implement code:
#include
#include
#define base 128
/* to store a data (consisting of key and value) in hash table array */
struct item
{
char *key;
char *value;
};
/* each hash table item has a flag (status) and data (consisting of key and value) */
struct hashtable_item
{
int flag;
/*
* flag = 0 : data does not exist
* flag = 1 : data exists at given array location
* flag = 2 : data was present at least once
*/
struct item *data;
};
struct hashtable_item *array;
int size = 0;
int max = 10;
/* this function returns corresponding index of the given key */
int hashcode(char *key)
{
return ((key[0]*base^2) % max+(key[1]*base)%max+key[2]%max)%max;
}
/* this function initializes the hash table array */
void init_array()
{
int i;
for (i = 0; i < max; i++)
{
array[i].flag = 0;
array[i].data = NULL;
}
}
/* this function inserts an element in the hash table */
void insert(char *key, char *value)
{
int index = hashcode(key);
int i = index;
int h = 1;
struct item *new_item = (struct item*) malloc(sizeof(struct item));
new_item->key = key;
new_item->value = value;
/* probing through the array until an empty space is found */
while (array[i].flag == 1)
{
if (array[i].data->key == key)
{
/* case when already present key matches the given key */
printf(" This key is already present in hash table, hence updating it's value ");
array[i].data->value = value;
return;
}
i = (i + (h * h)) % max;
h++;
if (i == index)
{
printf(" Hash table is full, cannot add more elements ");
return;
}
}
array[i].flag = 1;
array[i].data = new_item;
printf(" Key (%s) has been inserted ", key);
size++;
}
/* to remove an element form the hash table array */
void remove_element(char *key)
{
int index = hashcode(key);
int i = index;
int h = 1;
/* probing through the hash table until we reach at location where there had not been an element even once */
while (array[i].flag != 0)
{
if (array[i].flag == 1 && array[i].data->key == key)
{
/* case where data exists at the location and its key matches to the given key */
array[i].flag = 2;
array[i].data = NULL;
size--;
printf(" Key (%s) has been removed ", key);
return;
}
i = (i + (h * h)) % max;
h++;
if (i == index)
{
break;
}
}
printf(" Key does not exist ");
}
/* to display the contents of hash table */
void display()
{
int i;
for(i = 0; i < max; i++)
{
if (array[i].flag != 1)
{
printf(" Array[%d] has no elements ", i);
}
else
{
printf(" Array[%d] has elements %s (key) and %d (value) ", i, array[i].data->key, array[i].data->value);
}
}
}
int size_of_hashtable()
{
return size;
}
void main()
{
int choice, n, c;
char key, value;
system("cls");
array = (struct hashtable_item*) malloc(max * sizeof(struct hashtable_item*));
init_array();
do {
printf("Implementation of Hash Table in C with Quadratic Probing. ");
printf("MENU-: 1.Inserting item in the Hash table"
" 2.Removing item from the Hash table"
" 3.Check the size of Hash table"
" 4.Display Hash table"
" Please enter your choice-:");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Inserting element in Hash table ");
printf("Enter key and value-:\t");
scanf("%s %[^ ]", &key, &value);
insert(key, value);
break;
case 2:
printf("Deleting in Hash table Enter the key to delete-:");
scanf("%s", &key);
remove_element(key);
break;
case 3:
n = size_of_hashtable();
printf("Size of Hash table is-:%d ", n);
break;
case 4:
display();
break;
default:
printf("Wrong Input ");
}
printf(" Do you want to continue-:(press 1 for yes)\t");
scanf("%d", &c);
}while(c == 1);
getch();
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
