-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.c
96 lines (68 loc) · 1.41 KB
/
table.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
栈式 hash 符号表实现
*/
#include <assert.h>
#include <stdlib.h>
#include "util.h"
#include "table.h"
#define TAB_SIZE (137)
typedef struct entry_* entry;
struct TAB_table_{
entry hashtable[TAB_SIZE];
void* top;
};
struct entry_{
void *key;
void *value;
void *next; //解决hash冲突的指针
void *prevtop; //维护栈使用的指针
};
static entry Entry(void* key,void* value,void* next,void* prevtop){
entry ent = checked_malloc(sizeof(*ent));
ent->key=key;
ent->value=value;
ent->next=next;
ent->prevtop=prevtop;
return ent;
}
TAB_table TAB_empty(){
TAB_table t=checked_malloc(sizeof(*t));
for(int i=0;i<TAB_SIZE;i++){
t->hashtable[i]=NULL;
}
t->top=NULL;
return t;
}
void TAB_enter(TAB_table t,void *key,void * value){
int hash =(unsigned )key % TAB_SIZE;
entry ent = Entry(key,value,t->hashtable[hash],t->top);
t->hashtable[hash]=ent;
t->top=key; //这里要注意约定
}
/*
返回查找到的 value 或者是NULL (没找到的情况下)
*/
void* TAB_look(TAB_table t,void *key){
int hash =(unsigned )key % TAB_SIZE;
entry p = t->hashtable[hash];
while(p&&p->key!=key){
p=p->next;
}
if(p){
return p->value;
}else{
return NULL;
}
}
void *TAB_pop(TAB_table t) {
void *k; entry b; int index;
assert (t);
k = t->top;
assert (k);
index = ((unsigned)k) % TAB_SIZE;
b = t->hashtable[index];
assert(b);
t->hashtable[index] = b->next;
t->top=b->prevtop;
return b->key;
}