簡單哈希
原題:
使用鏈地址法(又稱拉鍊法)可以解決Hash中的衝突問題。其基本思想是:將具有相同哈希地址的記錄鏈成一個單鏈表,m個哈希地址就設m個單鏈表,然後用一個數組將m個單鏈表的表頭指針存儲起來,形成一個動態的結構(圖1)。
現在給定哈希函數爲Hash(key)= key mod 13,要求使用鏈地址法處理衝突,設有衝突的元素均插入表尾。要求建立起相應哈希表,並按一定格式打印。
輸入包含多組數據。對於每組數據:
第1行爲整數n(1 <=n <= 100), 代表key的總數。
接下來n行,每行一個整數,代表一個key。Key與key兩兩不相同。
當n=0的時候表示輸入結束。該行不作處理。
對於每一組數據輸出13行,每行表示某個哈希值下的Key。如果沒有任何key,則對應NULL。Key之間用空格隔開,每行行末沒有空格。格式如下面例子。
輸出建立好的hash表,比如下表
應輸出
0#22 11
1#89
2#NULL
3#3 47
4#37 92
5#16
6#50
7#29 7
8#8
9#NULL
10#10
34 7673 4664 5141 7711 8253 6868 5547 7644 2662 2757 37 2859 8723 9741 7529 778 2316 3035 2190 1842 288 106 9040 8942 9264 2648 7446 3805 5890 6729 4370 5350 5006 1101 0
0#7644 8723 1#2757 5890 5006 2#7711 7529 2316 288 106 4370 3#7673 4#6868 9741 5#9040 6#5141 3035 2190 7#5350 8#9264 6729 9#5547 1842 2648 3805 1101 10#4664 2662 7446 11#8253 37 778 8942 12#2859
利用鏈表棧跟stl棧實現:
// Problem#: 19590
// Submission#: 4967637
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<stack>
#include<cstring>
using namespace std;
struct node{
int data;
node* next;
node(int d= 0, node* n= NULL) {
data= d;
next= n;
}
};
struct head_list{
node *head;
};
//int path[20000005]= {0};
int main() {
int n, temp;
stack<int> s[13];
head_list ha[13];
while (cin>> n&& n) {
memset(ha, 0, sizeof(ha));
//memset(path, 0, sizeof(path));
for (int i= 0; i< n; i++) {
cin>> temp;
//if (path[temp+ 10000000]!= 0) continue;
//path[temp+ 10000000]= 1;
int m= temp% 13;
if (ha[m].head== NULL) {
ha[m].head= new node(temp, NULL);
} else {
node* t= new node(temp, ha[m].head);
ha[m].head= t;
}
}
for (int i= 0; i< 13; i++) {
cout<< i<< '#';
bool flag= true;
while (ha[i].head!= NULL) {
flag = false;
node *t= ha[i].head;
s[i].push(t->data);
ha[i].head= ha[i].head->next;
delete t;
}
if (flag== true) {
cout<< "NULL"<< endl;
} else {
while (!s[i].empty()) {
if (s[i].size()- 1!= 0) {
cout<< s[i].top()<< " ";
} else {
cout<< s[i].top()<< endl;
}
s[i].pop();
}
}
}
}
return 0;
}
利用隊列實現:
// Problem#: 19590
// Submission#: 4968016
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<cstring>
using namespace std;
struct node{
int data;
node* next;
node(int d= 0, node* n= NULL) {
data= d;
next= n;
}
};
struct head_list{
node *head, *tail;
head_list() {
head= tail= NULL;
}
};
int main() {
int n, temp;
head_list ha[13];
while (cin>> n&& n) {
memset(ha, 0, sizeof(ha));
for (int i= 0; i< n; i++) {
cin>> temp;
int m= temp% 13;
if (ha[m].tail== NULL) {
ha[m].head= ha[m].tail= new node(temp, NULL);
} else {
node* t= new node(temp, NULL);
ha[m].tail->next= t;
ha[m].tail= t;
}
}
for (int i= 0; i< 13; i++) {
cout<< i<< '#';
bool flag= true;
while (ha[i].head!= NULL) {
flag = false;
node *t= ha[i].head;
if (t!= ha[i].tail) {
cout<< t->data<< " ";
} else {
cout<< t->data<< endl;
}
ha[i].head= ha[i].head->next;
delete t;
}
if (flag== true) {
cout<< "NULL"<< endl;
}
}
}
return 0;
}