// 해시 테이블이기는하나 완벽하지 않다..
// 리스트 10칸에 10칸씩 리스트 되어야 맞는 것 같으나 그냥 리스트 10칸에서 다시 해시가 적용된다..
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define TABLE_SIZE 10;
typedef struct _node NODE;
struct _node
{
int key;
NODE *next;
};
int hash_func(int key)
{
int h;
h = key % TABLE_SIZE;
return h;
}
int hsc_init(NODE a[],int *np,int N)
{
int i;
for(i=0;i<N;i++)
a[i].next = NULL;
*np=0;
return 1;
}
NODE *hsc_insert(int key,NODE a[],int *np)
{
int tri;
NODE *t;
t = (NODE*)malloc(sizeof(NODE));
tri = hash_func(key);
t->next = a[tri].next; /* a[tri]는 테이블의 첫라인만 가리킬뿐 값은 들어있지않다. */
t->key = key; /* 새로운 key값이 들은 NODE를 만들어 기존 NODE보다 앞, 즉 a[tri]->next 에 삽입한다. */
a[tri].next = t;
printf("%d,,\n",tri);
(*np)++;
return t;
}
NODE *hsc_search(int key,NODE a[],int *np)
{
NODE *t;
t = a[hash_func(key)].next;
while(t !=NULL && t->key != key )
t = t->next;
printf("%d\n",t->key);
return t;
}
NODE *hsc_delete(int key,NODE a[], int *np)
{
NODE *p;
NODE *t;
if(*np > 0)
{
p = &a[hash_func(key)];
t = p->next;
while(t != NULL && t->key != key)
{
p = t;
t = t->next;
}
if( t == NULL)
return NULL;
p->next = t->next;
free(t);
(*np)--;
return p;
}
return NULL;
}
int hsc_deleteall(NODE a[],int *np,int N)
{
NODE *t,*p;
int i;
for( i=0;i<N;i++)
{
t = a[i].next;
while(t != NULL)
{
p = t;
t = t->next;
free(p);
}
}
*np = 0;
return 1;
}
void main()
{
NODE a[10];
NODE *tmp;
int np;
hsc_init(a,&np,10);
hsc_insert(1,a,&np);
hsc_insert(1,a,&np);
printf("*%d,%d\n%d\n",a[1].key,a[1].next->key,a[1].next->key );
//tmp = hsc_search(2,a,&np);
//printf("%d\n",tmp->key);
}
'프로그래밍 언어 > 프로그래밍' 카테고리의 다른 글
[유니티] 오브젝트 터치좌표로 이동하기 (0) | 2016.06.17 |
---|---|
유니티 점프 (0) | 2016.06.08 |
트리 (0) | 2009.01.18 |
연결 리스트 (문장<이름>) (0) | 2009.01.18 |
연결 리스트 (한 글자) (0) | 2009.01.18 |
댓글