您的位置:首页 > 其它

哈希表 除数余留 + 链地址 && 线性探测 && 平方探测

2017-01-17 21:02 423 查看
/*****************************
author: yomi
date: 17.1.17
mood:
ps:
*******************************/
/***
#include<iostream>
using namespace std;
const int maxn = 13;

struct hash_node{
int data;
hash_node *next;
};

int hash(int num){
return num % maxn;
}

void collision(hash_node *vec[], int elem, hash_node *new1){
if(vec[elem] == NULL){
vec[elem] = new1;
return;
}
new1->next = vec[elem];
vec[elem] = new1;
}

void hash_create(hash_node *vec[], int n){
int tmp;
for(int i=0; i<n; i++){
hash_node *p = new hash_node;
p->next = NULL;
cin >> p->data;
tmp = hash(p->data);
collision(vec, tmp, p);
}
}
void hash_print(hash_node *vec[]){
for(int i=0; i<maxn; i++){
hash_node *p;
cout << "vec[" << i << "]:";

if(vec[i] == NULL){
cout << "NULL" << endl;
continue;
}
p = vec[i];
while(p){
cout << p->data << "->";
p = p->next;
}
cout << "NULL" << endl;
}

}
void delete_hash_node(hash_node *vec[], int num){
int elem = hash(num);
int flag = 0;
if(vec[elem] == NULL){
cout << "not found!!!" << endl;
return;
}
hash_node *p;
p = vec[elem];
if(p->data == num){
vec[elem] = p->next;
delete p;

}
else{
hash_node *q;
q = vec[elem]->next;
while(q){
if(q->data == num){
p->next = q->next;
flag = 1;
delete q;
break;
}
p = p->next;
q = q->next;
}
if(flag == 0){
cout << "not found!!!" << endl;
}

}

}
int main(){
hash_node *vec[maxn];
for(int i=0; i<maxn; i++){
vec[i] = NULL;
}
int n;
cin >> n;
hash_create(vec, n);
hash_print(vec);
cout << "delete_hash_node" << endl;
while(1){
int num;
cin >> num;
delete_hash_node(vec, num);
hash_print(vec);
}

return 0;
}
**/
/**
12
19 14 23 01 68 20 84 27 55 11 10 79
vec[0]:NULL
vec[1]:79->27->1->14->NULL
vec[2]:NULL
vec[3]:55->68->NULL
vec[4]:NULL
vec[5]:NULL
vec[6]:84->19->NULL
vec[7]:20->NULL
vec[8]:NULL
vec[9]:NULL
vec[10]:10->23->NULL
vec[11]:11->NULL
vec[12]:NULL
delete_hash_node

27
vec[0]:NULL
vec[1]:79->1->14->NULL
vec[2]:NULL
vec[3]:55->68->NULL
vec[4]:NULL
vec[5]:NULL
vec[6]:84->19->NULL
vec[7]:20->NULL
vec[8]:NULL
vec[9]:NULL
vec[10]:10->23->NULL
vec[11]:11->NULL
vec[12]:NULL
10
vec[0]:NULL
vec[1]:79->1->14->NULL
vec[2]:NULL
vec[3]:55->68->NULL
vec[4]:NULL
vec[5]:NULL
vec[6]:84->19->NULL
vec[7]:20->NULL
vec[8]:NULL
vec[9]:NULL
vec[10]:23->NULL
vec[11]:11->NULL
vec[12]:NULL
100
not found!!!
vec[0]:NULL
vec[1]:79->1->14->NULL
vec[2]:NULL
vec[3]:55->68->NULL
vec[4]:NULL
vec[5]:NULL
vec[6]:84->19->NULL
vec[7]:20->NULL
vec[8]:NULL
vec[9]:NULL
vec[10]:23->NULL
vec[11]:11->NULL
vec[12]:NULL
10
not found!!!
vec[0]:NULL
vec[1]:79->1->14->NULL
vec[2]:NULL
vec[3]:55->68->NULL
vec[4]:NULL
vec[5]:NULL
vec[6]:84->19->NULL
vec[7]:20->NULL
vec[8]:NULL
vec[9]:NULL
vec[10]:23->NULL
vec[11]:11->NULL
vec[12]:NULL

**/

/**
#include<iostream>
using namespace std;
const int maxn = 5;
int a[maxn];

int _hash(int num){
return num % maxn;
}
void collision(int elem, int s){
if(elem == maxn){
elem = 0;
}
if(a[elem] == -10000){
a[elem] = s;
}
else{
collision(elem+1, s);
}
}
void create(){
int n, s;
int tmp;
cin >> n;
for(int i=0; i<n; i++){
cin >> s;
tmp = _hash(s);
collision(tmp, s);
}
}

void print(){
for(int i=0; i<maxn; i++){
cout << "[" << i  << "] :";
if(a[i] == -10000){
cout << "NULL";
}
else{
cout << a[i];
}
cout << endl;
}
}
int main(){
for(int i=0; i<maxn; i++){
a[i] = -10000;
}
create();
print();
return 0;
}
**/
/**
3
8 13 18
[0] :18
[1] :NULL
[2] :NULL
[3] :8
[4] :13

Process returned 0 (0x0)   execution time : 9.308 s
Press any key to continue.

**/

#include<iostream>
using namespace std;
const int maxn = 11;
int a[maxn];

int _hash(int num){
return num % maxn;
}

void collision(int elem, int s){
int conflic = 1;
int ii=0;
while(1){
if(a[elem] == -10000){
a[elem] = s;
break;
}
else {
elem = elem+conflic*conflic-ii*ii;
if((elem+conflic*conflic-ii*ii)>=maxn){
elem %= maxn;
}
ii++;
conflic++;
}
}
}

void create(){
int n, s;
int tmp;
cin >> n;
for(int i=0; i<n; i++){
cin >> s;
tmp = _hash(s);
collision(tmp, s);
}
}

void print(){
for(int i=0; i<maxn; i++){
cout << "[" << i  << "] :";
if(a[i] == -10000){
cout << "NULL";
}
else{
cout << a[i];
}
cout << endl;
}
}

void init(){
for(int i=0; i<maxn; i++){
a[i] = -10000;
}
}

int main(){
init();
create();
print();
return 0;
}

/**
9
47 7 29 11 16 92 22 8 3
[0] :11
[1] :22
[2] :NULL
[3] :47
[4] :92
[5] :16
[6] :3
[7] :7
[8] :29
[9] :8
[10] :NULL

Process returned 0 (0x0)   execution time : 119.109 s
Press any key to continue.

**/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: