프로그래머의 삶 Programmer's Life/C & C++

해쉬의 연결법

Oliver's World 2008. 11. 3. 14:05
728x90

#include
#include
#define EMPTY -1
#define TABLE_SIZE 10

typedef struct _node
{
int key;
struct _node *next;
}hash;

void HSC_init( hash a[] , int *ct )
{
int i;
for ( i = 0 ; inext = a[tr].next;
sc->key = input;
a[tr].next = sc;
(*ct)++;
}

else printf("input is end\n");
}

void HSC_print( hash a[], int *ct )
{
int i;
hash *t;
for ( i = 0; i < TABLE_SIZE ; i++)
{
t = a[i].next;
while ( t != NULL )
{
printf( "%-5d" , t->key );
t = t -> next;
}
printf("\n");
}
printf("DataCount:%d",*ct);
}

void HSC_deleteall( hash a[] ,int *ct )
{
hash *t,*p;
int i;
for ( i = 0 ; i < TABLE_SIZE ; i++ )
{
t = a[i].next;
while ( t != NULL )
{
p = t;
t = t->next;
free(p);
}
}
*ct = 0;
}


int main(void)
{
hash HASH[TABLE_SIZE];
int DataCount;
int INPUT;
HSC_init(HASH,&DataCount);

printf("please input: if 0 then end\n");
do{
scanf("%d",&INPUT);
HSC_insert(INPUT,HASH,&DataCount);
}while(INPUT != 0);


HSC_print(HASH,&DataCount);

HSC_deleteall(HASH,&DataCount);

return 0;
}


728x90