sicily 简单哈希

简单哈希

原题:

Description

 使用链地址法(又称拉链法)可以解决Hash中的冲突问题。其基本思想是:将具有相同哈希地址的记录链成一个单链表,m个哈希地址就设m个单链表,然后用一个数组将m个单链表的表头指针存储起来,形成一个动态的结构(图1)。

 

现在给定哈希函数为Hash(key)= key mod 13,要求使用链地址法处理冲突,设有冲突的元素均插入表尾。要求建立起相应哈希表,并按一定格式打印。

 

Input

输入包含多组数据。对于每组数据:

 第1行为整数n(1 <=n <= 100), 代表key的总数。
接下来n行,每行一个整数,代表一个key。Key与key两两不相同。

当n=0的时候表示输入结束。该行不作处理。

Output

对于每一组数据输出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

Sample Input
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
Sample Output
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;
}                                 





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值