-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHash.cpp
executable file
·153 lines (153 loc) · 3.4 KB
/
Hash.cpp
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <assert.h>
using namespace std;
const int defaultSize=100;
template <typename E, typename K>
struct ChainNode {
K key;
E data;
ChainNode<E,K> *link;
};
template <typename E,typename K>
class HashTable {
public:
HashTable(int d,int sz=defaultSize);
~HashTable(){delete []ht;}
bool Search(const K k1,E& e1);
bool Insert(const K k1,const E el);
bool Change(const K k1,const E e1);
bool Remove(const K k1,E& e1);
void Print();
private:
int divisor;
int TableSize;
ChainNode<E,K> **ht;
ChainNode<E,K> *FindPos(const K k1);
};
template <typename E,typename K>
HashTable<E,K>::HashTable(int d,int sz){
divisor=d;
TableSize=sz;
ht=new ChainNode<E,K> *[sz];
assert(ht!=NULL);
};
template <typename E,typename K>
ChainNode<E,K> *HashTable<E,K>::FindPos(const K k1) {
int j=k1%divisor;
ChainNode<E,K> *p=ht[j];
while (p!=NULL&&p->key!=k1)p=p->link;
return p;
};
template <typename E,typename K>
bool HashTable<E,K>::Search(const K k1,E &e1) {
ChainNode<E,K> *p=FindPos(k1);
if(p) {e1=p->data;return true;}
else return false;
};
template <typename E,typename K>
bool HashTable<E,K>::Insert(const K k1,const E e1) {
int j=k1%divisor;
ChainNode<E,K> *p=ht[j];
ChainNode<E,K> *q=new ChainNode<E,K>;
if(!q) return false;
q->key=k1;
q->data=e1;
q->link=NULL;
if (p) {
while (p->link!=NULL) p=p->link;
p->link=q;
}
else ht[j]=q;
return true;
};
template <typename E,typename K>
bool HashTable<E,K>::Change(const K k1,const E e1) {
ChainNode<E,K>* p=FindPos(k1);
if(p) {
p->data=e1;
return true;
}
else return false;
};
template <typename E,typename K>
bool HashTable<E,K>::Remove(const K k1,E& e1) {
int j=k1%divisor;
ChainNode<E,K> *p=ht[j];
if(p){
while (p->link!=NULL&&p->link->key!=k1)p=p->link;
if(p->link) {
ChainNode<E,K> *q=p->link;
p->link=q->link;
e1=q->data;
delete q;
return true;
}
}
return false;
};
template <typename E,typename K>
void HashTable<E,K>::Print() {
for (int i=0;i<divisor;i++){
ChainNode<E,K> *p=ht[i];
cout<<i<<':';
while(p!=NULL) {
cout<<'<'<<p->key<<','<<p->data<<"> ";
p=p->link;
}
cout<<endl;
}
};
int main() {
int d,sz,key,data;
cout<<"please input the divisor and the number of elements"<<endl;
cin>>d>>sz;
HashTable<int,int> hash(d,d);
cout<<"please input the ChainNode"<<endl;
//assum data == key
cin>>key;
data=key;
for(int i=0;i<sz-1;i++){
if(hash.Insert(key,data)) { cin>>key;data=key; }
else { cout<<"error"<<endl;break; }
}
if(!hash.Insert(key,data)) cout<<"error"<<endl;
while (1){
int index,key,data;
cout<<"what to do next?"<<endl;
cout<<"(1) Search,(2) Insert,(3) Change,(4) Remove,(5) Print,(0) Exit"<<endl;
cin>>index;
if(!index) break;
else
switch(index){
case 1:
cout<<"please input the key"<<endl;
cin>>key;
if(hash.Search(key,data)) cout<<data<<endl;
else cout<<"can't find"<<endl;
break;
case 2:
cout<<"please input the key and data"<<endl;
cin>>key>>data;
if(hash.Insert(key,data)) cout<<"success"<<endl;
else cout<<"fail"<<endl;
break;
case 3:
cout<<"please input the key and data"<<endl;
cin>>key>>data;
if(hash.Change(key,data)) cout<<"success"<<endl;
else cout<<"fail"<<endl;
break;
case 4:
cout<<"please input the key"<<endl;
cin>>key;
if(hash.Remove(key,data)) cout<<data<<endl;
else cout<<"can't find"<<endl;
break;
case 5:
hash.Print();
break;
default: break;
}
}
return 0;
}