hash simple linear

자료구조 2015. 5. 22. 15:26

#include <stdio.h>

#include <stdlib.h>

#define HASH_SIZE 10

int hash[HASH_SIZE];


int hf(int key)

{

return key%10;

}


void hashin(int k)

{

int f = hf(k);

int i;


if (hash[f] == -1)

{

hash[f] = k;

}

else

{

for (i= (f+1) % HASH_SIZE ; i != f ; i = (i+1) % HASH_SIZE )

{

if (hash[i] == -1)

break;

}


if (i ==f)

{// hash full

printf ("꽉참\n");

return;

}

else

hash[i] = k;


}

}


void search(int k)

{

int f = hf(k);

int i;


if (hash[f] == k)

{

printf ("있음\n");

}

else

{

for (i= (f+1) % HASH_SIZE ; i != f && hash[i] != -1 ; i = (i+1) % HASH_SIZE )

{

if (hash[i] == k)

break;

}


if (i ==f || hash[i] == -1)

{// hash full

printf ("없음\n");

return;

}

else

printf ("있음\n");

}

}



int main(void)

{

int a;

int i;


for (i=0;i<HASH_SIZE;i++)

{

hash[i] = -1;

}


hashin(55);

hashin(16);

hashin(45);

hashin(33);

hashin(78);

hashin(88);

hashin(98);


search(33);

search(45);

search(1);


return 0;

}

'자료구조' 카테고리의 다른 글

simple adjacency list  (0) 2015.05.22
binary search tree ( insert, kth smallest, inorder, delete by key)  (0) 2015.05.15
link list(single)  (0) 2015.05.01
boolean combination  (0) 2015.05.01
Posted by rjh
,