Skip to content

Commit 07cf57f

Browse files
authored
Merge pull request #139 from yang5220/master
先按照超时合并的评价来合并吧,如果还想继续修改,请re-open当前PR再提交变更。
2 parents fa1c13e + 58db862 commit 07cf57f

File tree

18 files changed

+1808
-0
lines changed

18 files changed

+1808
-0
lines changed

2017-1/ywy/Hash表/2017-06-18 (2).png

34.6 KB
Loading

2017-1/ywy/Hash表/Hash.c

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
#include"Hash.h"
2+
bool SuS(int n)
3+
{
4+
int i;
5+
if(n==2)
6+
{
7+
return TRUE;
8+
}
9+
for (i = 2; i < n; i++)
10+
{
11+
if(n%i==0)
12+
{
13+
return FALSE;
14+
}
15+
}
16+
return TRUE;
17+
}
18+
Status InitHashTable(HashTable*H,int size)
19+
{
20+
int i;
21+
ElemType temp;
22+
H->count = 0;
23+
H->maxsize = size;
24+
H->elem = (ElemType*)malloc(sizeof(ElemType)*size);
25+
if (!(H->elem))
26+
{
27+
return error;
28+
}
29+
for (i = 0; i < size; i++)
30+
{
31+
H->elem[i].key = UNKEY;
32+
H->elem[i].val = UNVAL;
33+
}
34+
srand((unsigned)time(NULL));
35+
for (i = 0; H->count<size/2; i++)//装载因子0.5
36+
{
37+
38+
temp.key = (int) rand() % size;
39+
temp.val = (int) rand() % size;
40+
InsertHashTable(H, temp);
41+
//printf("%d---%d",temp.key,temp.val);
42+
}
43+
return ok;
44+
}
45+
bool Empty(HashTable H)
46+
{
47+
if (H.count == 0)
48+
{
49+
return TRUE;
50+
}
51+
return FALSE;
52+
}
53+
bool Equal(KeyType a, KeyType b)
54+
{
55+
if(a==b)
56+
{
57+
return TRUE;
58+
}
59+
return FALSE;
60+
}
61+
int SearchHashTable(HashTable H, KeyType key, int*add, int*count)
62+
{
63+
*add = key% H.maxsize;
64+
*count = 0;//冲突次数
65+
while (H.elem[*add].key != UNKEY && !Equal(key, H.elem[*add].key))//发生冲突
66+
{
67+
if ((*add) < H.maxsize)
68+
{
69+
//与Hash表中发生冲突的元素
70+
printf("冲突: 关键字--值%d->%d\n", H.elem[*add].key, H.elem[*add].val);
71+
(*add)++;
72+
(*count)++;
73+
}
74+
else
75+
break;
76+
}
77+
78+
if (Equal(key, H.elem[*add].key))
79+
{
80+
return SUCCESS;
81+
}
82+
else
83+
{
84+
return UNSUCCESS;
85+
}
86+
}
87+
Status InsertHashTable(HashTable*H, ElemType e)
88+
{
89+
int add, count;
90+
if (SearchHashTable(*H, e.key, &add, &count) == SUCCESS)
91+
{
92+
return DUPLICATE;
93+
}
94+
else {
95+
if (add == H->maxsize || count > H->maxsize / 2)
96+
{
97+
ReHash(H);
98+
}
99+
H->elem[add] = e;
100+
H->count++;
101+
printf("插入[%d]:%d->%d", add, H->elem[add].key, H->elem[add].val);
102+
printf("碰撞次数:%d\n", count);
103+
return ok;
104+
}
105+
106+
}
107+
Status ReHash(HashTable *H)
108+
{
109+
int i, n;
110+
HashTable temp;
111+
if (Empty(*H))
112+
{
113+
return error;
114+
}
115+
n = 2 * H->maxsize;
116+
while (!SuS(n))
117+
{
118+
n++;
119+
}
120+
temp.maxsize = n;
121+
temp.count = H->count;
122+
temp.elem = (ElemType *)malloc(sizeof(ElemType)*n);
123+
for (i = 0; i < n; i++)
124+
{
125+
temp.elem[i].key = UNKEY;
126+
temp.elem[i].val = UNVAL;
127+
}
128+
129+
for (i = 0; i < H->maxsize; i++) {
130+
temp.elem[i].key = H->elem[i].key;
131+
temp.elem[i].val = H->elem[i].val;
132+
}
133+
134+
H->maxsize = temp.maxsize;
135+
136+
H->count = temp.count;
137+
138+
H->elem = (ElemType *)malloc(sizeof(ElemType)*n);
139+
140+
for (i = 0; i < n; i++)
141+
{
142+
H->elem[i].key = temp.elem[i].key;
143+
H->elem[i].val = temp.elem[i].val;
144+
}
145+
146+
free(temp.elem);
147+
return ok;
148+
}
149+
Status PrintHash(HashTable H)
150+
{
151+
int i;
152+
if(Empty(H))
153+
{
154+
return error;
155+
}
156+
printf("print hash\n");
157+
for (i = 0; i < H.maxsize; i++)
158+
{
159+
printf("[%d]: %d->%d\n", i, H.elem[i].key, H.elem[i].val);
160+
}
161+
return ok;
162+
}
163+
164+
165+
166+
167+

2017-1/ywy/Hash表/Hash.h

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#pragma once
2+
#include<stdio.h>
3+
#include<time.h>
4+
#include<stdlib.h>
5+
#include<math.h>
6+
#define SUCCESS 1
7+
#define UNSUCCESS 0
8+
#define DUPLICATE -1
9+
#define UNKEY -1
10+
#define UNVAL 0
11+
//#define HASHSIZE 30
12+
typedef int KeyType;
13+
typedef int ValueType;
14+
typedef struct _ElemType
15+
{
16+
KeyType key;//关键字
17+
ValueType val;//值
18+
#ifdef CHAINED_HASH
19+
struct _ElemType*next;
20+
#endif
21+
}ElemType;
22+
typedef struct
23+
{
24+
ElemType*elem;//存储哈希表元素
25+
int count;
26+
int maxsize;
27+
}HashTable;
28+
typedef enum
29+
{
30+
ok,
31+
error
32+
}Status;
33+
34+
typedef enum
35+
{
36+
FALSE,
37+
TRUE
38+
}bool;
39+
Status PrintHash(HashTable H);
40+
Status InitHashTable(HashTable*H,int size);
41+
Status InsertHashTable(HashTable*H, ElemType e);
42+
bool Empty(HashTable H);
43+
bool Equal(KeyType a, KeyType b);
44+
bool SuS(int n);
45+
int SearchHashTable(HashTable H, KeyType key, int*add, int*count);
46+
Status ReHash(HashTable *H);

2017-1/ywy/Hash表/test.c

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include"Hash.h"
2+
int main()
3+
4+
{
5+
int size;
6+
int i;
7+
KeyType key;
8+
HashTable H;
9+
size = 11;//
10+
srand((unsigned)time(NULL));
11+
12+
InitHashTable(&H, size);
13+
PrintHash(H);
14+
int add = 0;
15+
int count = 0;
16+
for (i = 0; i < H.count; i++)
17+
{
18+
key = rand() % size;
19+
if(SearchHashTable(H,key,&add,&count)==SUCCESS)
20+
{
21+
printf("find %d ", key);
22+
printf("address %d: \n", add);
23+
}
24+
else
25+
{
26+
printf("not find %d: \n", key);
27+
}
28+
}
29+
return 0;
30+
}

2017-1/ywy/图/2017-05-24.png

33.2 KB
Loading

0 commit comments

Comments
 (0)