-
Gabriela Acuña authored621f730f
//NO es mío! lo ocupe para estudiar, fuente: https://www.log2base2.com/algorithms/searching/open-hashing.html
#include<stdio.h>
#include<stdlib.h>
#define M 10
typedef struct nodo
{
int key;
struct nodo *next;
}nodo;
nodo *Ht[M];
void init()
{
int i;
for(i = 0; i < M; i++)
Ht[i] = NULL;
}
int hash(int k){
return k % M;
}
void insert(int value)
{
nodo *newN = malloc(sizeof(nodo));
newN->key = value;
newN->next = NULL;
int key = hash(value);
if(Ht[key] == NULL)
Ht[key] = newN;
//collision
else
{
//add the node at the end of Ht[key].
nodo *temp = Ht[key];
while(temp->next)
{
temp = temp->next;
}
temp->next = newN;
}
}
void print()
{
int i;
printf("~~~\n");
for(i = 0; i < M; i++)
{
nodo *temp = Ht[i];
printf("[%d]-->",i);
while(temp)
{
printf("%d -->",temp->key);
temp = temp->next;
}
printf("NULL\n");
}
printf("~~~\n");
}
int main()
{
71727374757677787980818283848586
init();
print();
insert(123);
insert(034);
insert(435);
insert(4369);
insert(956);
insert(654);
insert(344);
print();
return 0;
}