пытается реализовать словарь с использованием хеш-таблицы после большой отладки, застрявшей с этой ошибкой malloc
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/* to store a data (consisting of key and value) in hash table array */
struct item
{
char key[100];
char value[1000];
};
/* 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 = 10007;
int max = 10007;
/ * эта функция возвращает соответствующий индекс данного ключа * /
int hashcode(char* key,int TS)
{
return (key[0]+27*key[1]+729*key[2])%TS;
}
/* 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;
}
}
/ * эта функция вставляет элемент в хеш-таблицу * /
void insert(char* key, char* value,int TS)
{
int index = hashcode(key,TS);
int i = index;
int h = 1;
struct item *new_item = (struct item*) malloc(sizeof(struct item));
strcpy(new_item->key , key);
strcpy(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("\n This key is already present in hash table, hence updating it's value \n");
strcpy(array[i].data->value , value);
return;
}
i = (i + (h * h)) % max;
h++;
if (i == index)
{
printf("\n Hash table is full, cannot add more elements \n");
return;
}
}
array[i].flag = 1;
array[i].data = new_item;
printf("\n Key (%s) has been inserted\n", key);
size++;
}
/* to remove an element form the hash table array */
void remove_element(char* key,int TS)
{
int index = hashcode(key,TS);
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("\n Key (%s) has been removed \n", key);
return;
}
i = (i + (h * h)) % max;
h++;
if (i == index)
{
break;
}
}
printf("\n Key does not exist \n");
}
/* to display the contents of hash table */
void display()
{
int i;
for(i = 0; i < max; i++)
{
if (array[i].flag != 1)
{
printf("\n Array[%d] has no elements \n", i);
}
else
{
printf("\n Array[%d] has elements \n %s (key) and %s (value) \n", i, array[i].data->key, array[i].data->value);
}
}
}
int size_of_hashtable()
{
return size;
}
основная функция
void main()
{
int choice,n, c;
char key[100];
char value[1000];
int TS=10007;
array = (struct hashtable_item*) malloc(max * sizeof(struct hashtable_item*));
init_array();
do {
printf("Implementation of Hash Table in C with Quadratic Probing.\n\n");
printf("MENU-: \n1.Inserting item in the Hash table"
"\n2.Removing item from the Hash table"
"\n3.Check the size of Hash table"
"\n4.Display Hash table"
"\n\n Please enter your choice-:");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Inserting element in Hash table \n");
printf("Enter key:");
fgets(key,100, stdin);
printf("Enter value:");
fgets(value,1000, stdin);
insert(key, value,TS);
break;
case 2:
printf("Deleting in Hash table \n Enter the key to delete-:");
fgets(key,100, stdin);
remove_element(key,TS);
break;
case 3:
n = size_of_hashtable();
printf("Size of Hash table is-:%d\n", n);
break;
case 4:
display();
break;
default:
printf("Wrong Input\n");
}
printf("\n Do you want to continue-:(press 1 for yes)\t");
scanf("%d", &c);
}while(c == 1);
}
Реализация хеш-таблицы в C с квадратичным зондированием. Вот так выглядит вывод:
MENU-:
1.Inserting item in the Hash table
2.Removing item from the Hash table
3. Check the size of the Hash table
4.Display Hash table
Please enter your choice-:1
Inserting element in Hash table
Enter key:Enter value:john helllo we aer
a.out: malloc.c:2374: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end `enter code here`& pagemask) == 0)' failed.
Aborted (core dumped)