Skip to content
Merged

#139

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added 2017-1/ywy/Hash表/2017-06-18 (2).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
167 changes: 167 additions & 0 deletions 2017-1/ywy/Hash表/Hash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
#include"Hash.h"
bool SuS(int n)
{
int i;
if(n==2)
{
return TRUE;
}
for (i = 2; i < n; i++)
{
if(n%i==0)
{
return FALSE;
}
}
return TRUE;
}
Status InitHashTable(HashTable*H,int size)
{
int i;
ElemType temp;
H->count = 0;
H->maxsize = size;
H->elem = (ElemType*)malloc(sizeof(ElemType)*size);
if (!(H->elem))
{
return error;
}
for (i = 0; i < size; i++)
{
H->elem[i].key = UNKEY;
H->elem[i].val = UNVAL;
}
srand((unsigned)time(NULL));
for (i = 0; H->count<size/2; i++)//װ������0.5
{

temp.key = (int) rand() % size;
temp.val = (int) rand() % size;
InsertHashTable(H, temp);
//printf("%d---%d",temp.key,temp.val);
}
return ok;
}
bool Empty(HashTable H)
{
if (H.count == 0)
{
return TRUE;
}
return FALSE;
}
bool Equal(KeyType a, KeyType b)
{
if(a==b)
{
return TRUE;
}
return FALSE;
}
int SearchHashTable(HashTable H, KeyType key, int*add, int*count)
{
*add = key% H.maxsize;
*count = 0;//��ͻ����
while (H.elem[*add].key != UNKEY && !Equal(key, H.elem[*add].key))//������ͻ
{
if ((*add) < H.maxsize)
{
//��Hash���з�����ͻ��Ԫ��
printf("��ͻ�� �ؼ���--ֵ%d->%d\n", H.elem[*add].key, H.elem[*add].val);
(*add)++;
(*count)++;
}
else
break;
}

if (Equal(key, H.elem[*add].key))
{
return SUCCESS;
}
else
{
return UNSUCCESS;
}
}
Status InsertHashTable(HashTable*H, ElemType e)
{
int add, count;
if (SearchHashTable(*H, e.key, &add, &count) == SUCCESS)
{
return DUPLICATE;
}
else {
if (add == H->maxsize || count > H->maxsize / 2)
{
ReHash(H);
}
H->elem[add] = e;
H->count++;
printf("����[%d]:%d->%d", add, H->elem[add].key, H->elem[add].val);
printf("��ײ����:%d\n", count);
return ok;
}

}
Status ReHash(HashTable *H)
{
int i, n;
HashTable temp;
if (Empty(*H))
{
return error;
}
n = 2 * H->maxsize;
while (!SuS(n))
{
n++;
}
temp.maxsize = n;
temp.count = H->count;
temp.elem = (ElemType *)malloc(sizeof(ElemType)*n);
for (i = 0; i < n; i++)
{
temp.elem[i].key = UNKEY;
temp.elem[i].val = UNVAL;
}

for (i = 0; i < H->maxsize; i++) {
temp.elem[i].key = H->elem[i].key;
temp.elem[i].val = H->elem[i].val;
}

H->maxsize = temp.maxsize;

H->count = temp.count;

H->elem = (ElemType *)malloc(sizeof(ElemType)*n);

for (i = 0; i < n; i++)
{
H->elem[i].key = temp.elem[i].key;
H->elem[i].val = temp.elem[i].val;
}

free(temp.elem);
return ok;
}
Status PrintHash(HashTable H)
{
int i;
if(Empty(H))
{
return error;
}
printf("print hash\n");
for (i = 0; i < H.maxsize; i++)
{
printf("[%d]: %d->%d\n", i, H.elem[i].key, H.elem[i].val);
}
return ok;
}





46 changes: 46 additions & 0 deletions 2017-1/ywy/Hash表/Hash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<math.h>
#define SUCCESS 1
#define UNSUCCESS 0
#define DUPLICATE -1
#define UNKEY -1
#define UNVAL 0
//#define HASHSIZE 30
typedef int KeyType;
typedef int ValueType;
typedef struct _ElemType
{
KeyType key;//�ؼ���
ValueType val;//ֵ
#ifdef CHAINED_HASH
struct _ElemType*next;
#endif
}ElemType;
typedef struct
{
ElemType*elem;//�洢��ϣ��Ԫ��
int count;
int maxsize;
}HashTable;
typedef enum
{
ok,
error
}Status;

typedef enum
{
FALSE,
TRUE
}bool;
Status PrintHash(HashTable H);
Status InitHashTable(HashTable*H,int size);
Status InsertHashTable(HashTable*H, ElemType e);
bool Empty(HashTable H);
bool Equal(KeyType a, KeyType b);
bool SuS(int n);
int SearchHashTable(HashTable H, KeyType key, int*add, int*count);
Status ReHash(HashTable *H);
30 changes: 30 additions & 0 deletions 2017-1/ywy/Hash表/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include"Hash.h"
int main()

{
int size;
int i;
KeyType key;
HashTable H;
size = 11;//
srand((unsigned)time(NULL));

InitHashTable(&H, size);
PrintHash(H);
int add = 0;
int count = 0;
for (i = 0; i < H.count; i++)
{
key = rand() % size;
if(SearchHashTable(H,key,&add,&count)==SUCCESS)
{
printf("find %d ", key);
printf("address %d: \n", add);
}
else
{
printf("not find %d: \n", key);
}
}
return 0;
}
Binary file added 2017-1/ywy/图/2017-05-24.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading