При добавлении в хеш таблицу нового элемента, все остальные данные меняются на последний - C (СИ)
Формулировка задачи:
При добавлении в хеш таблицу нового элемента, все остальные данные меняются на последний. Я так понял, ошибка в выделении памяти. Как это исправить?
пример вводимых данных:
127.0.1.2 localhost
127.0.1.3 fr.a2dfp.net
127.0.1.4 m.fr.a2dfp.net
127.0.1.5 ad.a8.net
127.0.1.6 asy.a8ww.net
127.0.1.7 abcstats.com
127.0.1.8 a.abv.bg
127.0.1.9 adserver.abv.bg
127.0.1.10 adv.abv.bg
127.0.1.11 bimg.abv.bg
и вывод:
bimg.abv.bg
bimg.abv.bg
Для продолжения нажмите любую клавишу . . .
#include <stdio.h>
#include <stdlib.h>
typedef int keyType; /* type of key */
typedef int hashIndexType; /* index into hash table */
typedef enum {
STATUS_OK,
STATUS_MEM_EXHAUSTED,
STATUS_KEY_NOT_FOUND
} statusEnum;
typedef struct nodeTag {
struct nodeTag *next; /* next node */
keyType key; /* key */
char* rec; /* user data */
} nodeType;
nodeType **hashTable;
int hashTableSize;
void initialize()
{
int i;
hashTableSize = 2000;
hashTable = (nodeType**) malloc(sizeof(nodeType)*hashTableSize);
for(i = 0; i < hashTableSize; i++)
hashTable[i] = NULL;
}
hashIndexType hash(keyType key) {
return (key % hashTableSize);
}
statusEnum insertNode(keyType key, char* rec) {
nodeType *p, *p0;
hashIndexType bucket;
bucket = hash(key);
if ((p = (nodeType*) malloc(sizeof(nodeType))) == 0)
return STATUS_MEM_EXHAUSTED;
p0 = hashTable[bucket];
hashTable[bucket] = p;
p->next = p0;
p->key = key;
p->rec = rec;
return STATUS_OK;
}
statusEnum deleteNode(keyType key) {
nodeType *p0, *p;
hashIndexType bucket;
/* find node */
p0 = 0;
bucket = hash(key);
p = hashTable[bucket];
while (p && (p->key != key)) {
p0 = p;
p = p->next;
}
if (!p) return STATUS_KEY_NOT_FOUND;
/* p designates node to delete, remove it from list */
if (p0)
/* not first node, p0 points to previous node */
p0->next = p->next;
else
/* first node on chain */
hashTable[bucket] = p->next;
free (p);
return STATUS_OK;
}
char* find(keyType key) {
nodeType *p;
p = hashTable[hash(key)];
while (p && (p->key != key))
p = p->next;
if (!p) return NULL;
return p->rec;
}
int main() {
char* a;
char data[50];
char ip[30];
int i, j, b;
FILE *mf;
initialize();
mf = fopen("hosts", "r");
for(i = 0; i < 10; i++){
fscanf(mf, "%s", ip);
fscanf(mf, "%s", data);
b = 0;
for(j = 0; ip[j];j++)
{
if(ip[j] == '.')
continue;
b = b*10 + ip[j] - 48;
}
insertNode(b,data);
}
if((a = find(127012)) != NULL)
printf("%s\n",a);
if((a = find(127013)) != NULL)
printf("%s\n",a);
return 0;
}Решение задачи: «При добавлении в хеш таблицу нового элемента, все остальные данные меняются на последний»
textual
Листинг программы
127.0.0.1 007guard.com 127.0.0.1 008i.com 127.0.0.1 008k.com 127.0.0.1 00hq.com 127.0.0.1 010402.com 127.0.0.1 012.net 127.0.0.1 012.net/updates 127.0.0.1 032439.com 127.0.0.1 06523e7588bb41464846d2dee8f95c54633a6873.googledrive.com 127.0.0.1 0scan.com 127.0.0.1 1-2005-search.com 127.0.0.1 1-domains-registrations.com 127.0.0.1 1.supertestirovanie.com 127.0.0.1 1000gratisproben.com 127.0.0.1 1000zakazov.ru 127.0.0.1 1001namen.com 127.0.0.1 100888290cs.com 127.0.0.1 100sexlinks.com 127.0.0.1 10sek.com 127.0.0.1 123fporn.info