#include <stdlib.h>
#include<stdio.h>
#include<time.h>
using namespace std;
typedef struct Hash_Node {
int data;
int count;
Hash_Node * next;
}node;
#define HASH_SIZE 1024
node *hash_table[HASH_SIZE];
int hash_index(int num) {
return num % HASH_SIZE;
}
node * make_node(int num) {
node *item = new node();
item->data = num;
item->count = 1;
item->next = NULL;
return item;
}
void insert(int num) {
int index = hash_index(num);
node *p = hash_table[index];
if(NULL == p) {
hash_table[index] = make_node(num);
} else {
bool found = false;
while(p) {
if(num == p->data) {
p->count++;
found = true;
break;
}
p=p->next;
}
if(!found) {
node *item = make_node(num);
item->next = hash_table[index];
hash_table[index] = item;
}
}
}
void output_hash() {
node *p = NULL;
for(int i=0;i<HASH_SIZE;i++) {
p = hash_table[i];
if(p) {
printf(" index %d ==> ",i);
while(p) {
printf(" (num: %d, count: %d)-->", p->data,p->count);
p=p->next;
}
printf("\n");
}
}
}
int main(int argc, char *argv[])
{
srand((int)time(0));
for(int i=0;i<1000;i++) {
insert(rand());
}
output_hash();
system("PAUSE");
return 0;
}
index 593 ==> (num: 29265, count: 1)--> (num: 9809, count: 1)-->
index 594 ==> (num: 27218, count: 1)--> (num: 9810, count: 1)-->
index 595 ==> (num: 19027, count: 1)-->
index 596 ==> (num: 21076, count: 1)--> (num: 20052, count: 1)-->
index 597 ==> (num: 21077, count: 1)--> (num: 6741, count: 1)-->
index 598 ==> (num: 25174, count: 1)--> (num: 28246, count: 1)--> (num: 23126
count: 1)-->
index 599 ==> (num: 12887, count: 1)-->
index 603 ==> (num: 14939, count: 1)-->
index 604 ==> (num: 26204, count: 1)-->
index 605 ==> (num: 5725, count: 1)--> (num: 13917, count: 1)-->
index 607 ==> (num: 3679, count: 1)-->
index 610 ==> (num: 14946, count: 1)--> (num: 19042, count: 1)-->
index 611 ==> (num: 16995, count: 1)-->
index 613 ==> (num: 32357, count: 1)-->
index 614 ==> (num: 614, count: 1)-->
index 615 ==> (num: 21095, count: 1)-->
index 619 ==> (num: 9835, count: 1)-->
index 622 ==> (num: 12910, count: 1)-->
index 623 ==> (num: 18031, count: 1)-->
index 625 ==> (num: 3697, count: 1)-->
index 626 ==> (num: 30322, count: 1)-->
index 627 ==> (num: 30323, count: 1)--> (num: 19059, count: 1)-->
index 629 ==> (num: 14965, count: 1)--> (num: 21109, count: 1)-->
index 630 ==> (num: 10870, count: 1)--> (num: 2678, count: 1)-->
index 631 ==> (num: 31351, count: 1)-->
index 632 ==> (num: 4728, count: 1)-->
index 634 ==> (num: 19066, count: 1)-->
index 635 ==> (num: 3707, count: 1)-->
index 637 ==> (num: 15997, count: 1)-->
index 638 ==> (num: 5758, count: 1)-->
index 639 ==> (num: 22143, count: 1)-->
index 640 ==> (num: 14976, count: 1)--> (num: 12928, count: 1)-->
index 641 ==> (num: 26241, count: 1)-->
index 646 ==> (num: 28294, count: 1)-->
index 647 ==> (num: 26247, count: 1)-->
index 648 ==> (num: 17032, count: 1)-->
index 649 ==> (num: 17033, count: 1)-->
index 651 ==> (num: 9867, count: 1)-->
index 653 ==> (num: 6797, count: 1)--> (num: 2701, count: 1)--> (num: 21133,
ount: 1)-->
index 654 ==> (num: 6798, count: 1)--> (num: 14990, count: 1)--> (num: 12942,
count: 1)-->
index 656 ==> (num: 656, count: 1)-->
index 657 ==> (num: 11921, count: 1)-->
index 658 ==> (num: 17042, count: 2)-->
index 660 ==> (num: 7828, count: 1)-->
index 661 ==> (num: 24213, count: 1)-->
index 662 ==> (num: 18070, count: 1)--> (num: 1686, count: 1)-->
index 664 ==> (num: 18072, count: 1)-->
index 665 ==> (num: 24217, count: 1)-->
index 666 ==> (num: 16026, count: 1)-->
index 668 ==> (num: 15004, count: 1)--> (num: 19100, count: 1)-->
index 670 ==> (num: 1694, count: 1)-->
index 671 ==> (num: 7839, count: 1)-->
index 673 ==> (num: 27297, count: 1)--> (num: 28321, count: 1)-->
index 674 ==> (num: 27298, count: 1)--> (num: 24226, count: 1)-->
index 676 ==> (num: 26276, count: 1)-->
index 677 ==> (num: 26277, count: 1)-->
index 678 ==> (num: 27302, count: 1)-->
index 681 ==> (num: 26281, count: 1)-->
index 682 ==> (num: 9898, count: 1)--> (num: 19114, count: 1)-->
index 683 ==> (num: 30379, count: 1)-->
index 684 ==> (num: 23212, count: 1)-->
index 687 ==> (num: 15023, count: 1)--> (num: 28335, count: 1)--> (num: 24239
count: 1)-->
index 688 ==> (num: 23216, count: 1)--> (num: 8880, count: 1)-->
index 692 ==> (num: 3764, count: 1)--> (num: 1716, count: 1)--> (num: 25268,
ount: 1)-->
index 693 ==> (num: 23221, count: 1)-->
index 694 ==> (num: 1718, count: 1)-->
index 696 ==> (num: 12984, count: 1)--> (num: 27320, count: 1)-->
index 699 ==> (num: 17083, count: 2)--> (num: 699, count: 1)-->
index 703 ==> (num: 21183, count: 1)-->
index 704 ==> (num: 11968, count: 1)-->
index 705 ==> (num: 1729, count: 1)-->
index 706 ==> (num: 23234, count: 1)-->
index 708 ==> (num: 24260, count: 1)--> (num: 26308, count: 2)-->
index 709 ==> (num: 16069, count: 1)--> (num: 8901, count: 1)-->
index 713 ==> (num: 13001, count: 1)--> (num: 8905, count: 1)--> (num: 15049,
count: 1)-->
index 714 ==> (num: 30410, count: 1)--> (num: 4810, count: 1)-->
index 716 ==> (num: 14028, count: 1)-->
index 717 ==> (num: 22221, count: 1)-->
index 718 ==> (num: 14030, count: 1)--> (num: 21198, count: 1)--> (num: 1742,
count: 1)--> (num: 30414, count: 1)-->
index 719 ==> (num: 26319, count: 1)--> (num: 11983, count: 1)-->
index 721 ==> (num: 13009, count: 1)--> (num: 22225, count: 1)-->
index 722 ==> (num: 16082, count: 1)-->
index 726 ==> (num: 29398, count: 1)-->
index 728 ==> (num: 19160, count: 1)--> (num: 14040, count: 1)-->
index 729 ==> (num: 28377, count: 1)-->
index 731 ==> (num: 4827, count: 1)-->
index 732 ==> (num: 29404, count: 1)-->
index 737 ==> (num: 27361, count: 1)--> (num: 16097, count: 1)-->
index 741 ==> (num: 29413, count: 1)-->
index 743 ==> (num: 20199, count: 1)-->
index 744 ==> (num: 9960, count: 1)-->
index 747 ==> (num: 20203, count: 1)-->
index 748 ==> (num: 27372, count: 1)-->
index 751 ==> (num: 19183, count: 1)--> (num: 30447, count: 1)--> (num: 26351
count: 1)-->
index 752 ==> (num: 752, count: 1)--> (num: 7920, count: 1)-->
index 753 ==> (num: 4849, count: 1)-->
index 754 ==> (num: 2802, count: 1)-->
index 755 ==> (num: 31475, count: 1)-->
index 756 ==> (num: 2804, count: 1)-->
index 758 ==> (num: 8950, count: 1)--> (num: 14070, count: 1)--> (num: 31478,
count: 1)-->
index 760 ==> (num: 19192, count: 1)-->
index 763 ==> (num: 25339, count: 1)--> (num: 4859, count: 1)-->
index 764 ==> (num: 16124, count: 1)-->
index 766 ==> (num: 20222, count: 1)-->
index 767 ==> (num: 29439, count: 1)-->
index 768 ==> (num: 21248, count: 2)-->
index 769 ==> (num: 12033, count: 1)-->
index 770 ==> (num: 31490, count: 1)-->
index 773 ==> (num: 11013, count: 1)-->
index 775 ==> (num: 26375, count: 1)--> (num: 14087, count: 1)--> (num: 775,
ount: 1)--> (num: 5895, count: 1)-->
index 778 ==> (num: 30474, count: 1)-->
index 779 ==> (num: 13067, count: 1)-->
index 780 ==> (num: 4876, count: 1)-->
index 781 ==> (num: 29453, count: 1)-->
index 783 ==> (num: 32527, count: 1)-->
index 784 ==> (num: 28432, count: 1)-->
index 788 ==> (num: 28436, count: 1)--> (num: 1812, count: 1)-->
index 790 ==> (num: 11030, count: 1)--> (num: 790, count: 2)-->
index 791 ==> (num: 8983, count: 1)--> (num: 26391, count: 1)-->
index 793 ==> (num: 18201, count: 1)-->
index 794 ==> (num: 28442, count: 1)-->
index 795 ==> (num: 31515, count: 1)-->
index 796 ==> (num: 31516, count: 1)-->
index 797 ==> (num: 14109, count: 1)-->
index 799 ==> (num: 7967, count: 1)-->
index 800 ==> (num: 8992, count: 1)-->
index 801 ==> (num: 13089, count: 1)-->
index 802 ==> (num: 32546, count: 1)-->
index 805 ==> (num: 15141, count: 1)--> (num: 24357, count: 1)-->
index 807 ==> (num: 19239, count: 1)-->
index 808 ==> (num: 13096, count: 1)-->
index 809 ==> (num: 23337, count: 1)-->
index 810 ==> (num: 2858, count: 1)-->
index 812 ==> (num: 1836, count: 1)--> (num: 14124, count: 1)-->
index 813 ==> (num: 813, count: 1)--> (num: 24365, count: 1)--> (num: 17197,
ount: 1)-->
index 815 ==> (num: 22319, count: 1)--> (num: 19247, count: 1)-->
index 816 ==> (num: 7984, count: 2)-->
index 818 ==> (num: 25394, count: 1)-->
index 820 ==> (num: 25396, count: 1)-->
index 821 ==> (num: 5941, count: 1)-->
index 822 ==> (num: 17206, count: 1)--> (num: 13110, count: 1)--> (num: 3894,
count: 1)-->
index 826 ==> (num: 25402, count: 1)-->
index 827 ==> (num: 827, count: 1)--> (num: 5947, count: 1)-->
index 829 ==> (num: 10045, count: 1)--> (num: 15165, count: 1)-->
index 831 ==> (num: 24383, count: 1)-->
index 834 ==> (num: 32578, count: 1)-->
index 836 ==> (num: 21316, count: 1)--> (num: 3908, count: 1)-->
index 837 ==> (num: 25413, count: 1)-->
index 838 ==> (num: 9030, count: 1)-->
index 839 ==> (num: 23367, count: 1)--> (num: 25415, count: 1)--> (num: 21319
count: 1)-->
index 840 ==> (num: 840, count: 1)--> (num: 20296, count: 1)--> (num: 25416,
ount: 2)-->
index 841 ==> (num: 23369, count: 1)-->
index 842 ==> (num: 8010, count: 1)--> (num: 28490, count: 1)--> (num: 24394,
count: 1)-->
index 843 ==> (num: 21323, count: 1)-->
index 845 ==> (num: 16205, count: 1)-->
index 846 ==> (num: 846, count: 1)--> (num: 2894, count: 1)--> (num: 12110, c
unt: 1)-->
index 847 ==> (num: 847, count: 1)--> (num: 16207, count: 1)-->
index 850 ==> (num: 17234, count: 1)-->
index 852 ==> (num: 10068, count: 1)--> (num: 29524, count: 1)-->
index 853 ==> (num: 22357, count: 1)--> (num: 15189, count: 1)--> (num: 1877,
count: 1)-->
index 854 ==> (num: 6998, count: 1)--> (num: 3926, count: 1)--> (num: 20310,
ount: 1)-->
index 855 ==> (num: 32599, count: 1)-->
index 856 ==> (num: 23384, count: 1)--> (num: 9048, count: 1)-->
index 857 ==> (num: 2905, count: 1)-->
index 859 ==> (num: 16219, count: 1)-->
index 860 ==> (num: 26460, count: 1)--> (num: 12124, count: 1)--> (num: 29532
count: 1)-->
index 862 ==> (num: 10078, count: 1)--> (num: 22366, count: 1)-->
index 865 ==> (num: 24417, count: 1)-->
index 866 ==> (num: 13154, count: 1)-->
index 869 ==> (num: 23397, count: 1)-->
index 870 ==> (num: 3942, count: 1)-->
index 871 ==> (num: 9063, count: 1)-->
index 872 ==> (num: 27496, count: 1)--> (num: 19304, count: 1)--> (num: 26472
count: 1)-->
index 875 ==> (num: 20331, count: 1)--> (num: 8043, count: 1)--> (num: 4971,
ount: 1)-->
index 876 ==> (num: 24428, count: 1)-->
index 878 ==> (num: 4974, count: 1)-->
index 880 ==> (num: 8048, count: 1)--> (num: 1904, count: 1)-->
index 883 ==> (num: 2931, count: 1)-->
index 884 ==> (num: 1908, count: 1)-->
index 885 ==> (num: 30581, count: 1)-->
index 886 ==> (num: 7030, count: 1)-->
index 887 ==> (num: 29559, count: 1)-->
index 888 ==> (num: 8056, count: 1)-->
index 889 ==> (num: 16249, count: 1)-->
index 890 ==> (num: 23418, count: 1)-->
index 893 ==> (num: 23421, count: 1)--> (num: 11133, count: 1)-->
index 894 ==> (num: 13182, count: 1)--> (num: 8062, count: 1)-->
index 895 ==> (num: 13183, count: 1)--> (num: 32639, count: 1)--> (num: 29567
count: 1)--> (num: 10111, count: 1)-->
index 897 ==> (num: 27521, count: 1)--> (num: 28545, count: 1)-->
index 899 ==> (num: 17283, count: 1)-->
index 900 ==> (num: 9092, count: 1)--> (num: 16260, count: 1)--> (num: 6020,
ount: 1)-->
index 903 ==> (num: 16263, count: 1)-->
index 908 ==> (num: 24460, count: 1)-->
index 909 ==> (num: 25485, count: 1)-->
index 910 ==> (num: 12174, count: 1)--> (num: 19342, count: 1)-->
index 911 ==> (num: 14223, count: 1)--> (num: 7055, count: 1)-->
index 912 ==> (num: 16272, count: 1)-->
index 915 ==> (num: 31635, count: 1)--> (num: 27539, count: 1)--> (num: 3987,
count: 1)-->
index 917 ==> (num: 28565, count: 2)-->
index 919 ==> (num: 2967, count: 1)--> (num: 8087, count: 1)--> (num: 30615,
ount: 1)-->
index 920 ==> (num: 14232, count: 1)--> (num: 30616, count: 1)-->
index 921 ==> (num: 30617, count: 1)-->
index 922 ==> (num: 14234, count: 1)--> (num: 31642, count: 1)-->
index 924 ==> (num: 29596, count: 1)-->
index 925 ==> (num: 17309, count: 1)--> (num: 925, count: 1)-->
index 929 ==> (num: 4001, count: 1)-->
index 930 ==> (num: 16290, count: 1)--> (num: 20386, count: 1)-->
index 931 ==> (num: 14243, count: 1)-->
index 932 ==> (num: 32676, count: 1)--> (num: 11172, count: 1)-->
index 933 ==> (num: 29605, count: 1)--> (num: 933, count: 1)-->
index 934 ==> (num: 26534, count: 1)-->
index 935 ==> (num: 7079, count: 1)-->
index 936 ==> (num: 5032, count: 1)--> (num: 7080, count: 1)--> (num: 26536,
ount: 1)-->
index 937 ==> (num: 27561, count: 1)-->
index 938 ==> (num: 8106, count: 1)-->
index 939 ==> (num: 28587, count: 1)-->
index 940 ==> (num: 6060, count: 1)-->
index 942 ==> (num: 32686, count: 1)-->
index 943 ==> (num: 24495, count: 1)-->
index 944 ==> (num: 8112, count: 1)-->
index 946 ==> (num: 946, count: 1)--> (num: 20402, count: 1)-->
index 948 ==> (num: 4020, count: 1)--> (num: 1972, count: 1)--> (num: 20404,
ount: 1)-->
index 949 ==> (num: 19381, count: 1)-->
index 950 ==> (num: 28598, count: 1)-->
index 951 ==> (num: 18359, count: 1)-->
index 952 ==> (num: 12216, count: 1)-->
index 953 ==> (num: 19385, count: 1)--> (num: 30649, count: 1)-->
index 960 ==> (num: 18368, count: 1)-->
index 961 ==> (num: 23489, count: 1)-->
index 963 ==> (num: 963, count: 1)-->
index 965 ==> (num: 24517, count: 1)--> (num: 16325, count: 1)-->
index 966 ==> (num: 15302, count: 1)-->
index 967 ==> (num: 16327, count: 1)-->
index 968 ==> (num: 17352, count: 1)-->
index 969 ==> (num: 1993, count: 1)--> (num: 28617, count: 1)-->
index 971 ==> (num: 11211, count: 1)-->
index 973 ==> (num: 13261, count: 1)-->
index 974 ==> (num: 10190, count: 1)-->
index 977 ==> (num: 28625, count: 1)-->
index 980 ==> (num: 28628, count: 1)--> (num: 29652, count: 1)--> (num: 25556
count: 1)-->
index 981 ==> (num: 18389, count: 1)--> (num: 28629, count: 1)-->
index 982 ==> (num: 22486, count: 1)-->
index 984 ==> (num: 24536, count: 1)-->
index 985 ==> (num: 23513, count: 1)-->
index 986 ==> (num: 21466, count: 1)--> (num: 25562, count: 1)-->
index 987 ==> (num: 26587, count: 1)-->
index 988 ==> (num: 14300, count: 1)--> (num: 988, count: 1)-->
index 992 ==> (num: 20448, count: 1)--> (num: 15328, count: 1)--> (num: 31712
count: 1)-->
index 995 ==> (num: 27619, count: 1)--> (num: 6115, count: 1)-->
index 996 ==> (num: 10212, count: 1)--> (num: 29668, count: 1)--> (num: 4068,
count: 1)-->
index 997 ==> (num: 20453, count: 1)-->
index 999 ==> (num: 23527, count: 1)--> (num: 15335, count: 1)--> (num: 7143,
count: 1)--> (num: 26599, count: 1)-->
index 1000 ==> (num: 9192, count: 1)-->
index 1002 ==> (num: 7146, count: 1)--> (num: 18410, count: 2)-->
index 1006 ==> (num: 32750, count: 1)--> (num: 13294, count: 1)--> (num: 1841
, count: 1)-->
index 1011 ==> (num: 1011, count: 1)--> (num: 9203, count: 1)--> (num: 8179,
ount: 2)-->
index 1012 ==> (num: 11252, count: 1)--> (num: 15348, count: 1)--> (num: 2251
, count: 1)-->
index 1013 ==> (num: 12277, count: 1)-->
index 1014 ==> (num: 1014, count: 1)-->
index 1015 ==> (num: 30711, count: 1)-->
index 1017 ==> (num: 25593, count: 1)-->
index 1019 ==> (num: 13307, count: 1)-->
index 1021 ==> (num: 9213, count: 1)-->
index 1022 ==> (num: 1022, count: 1)-->
index 1023 ==> (num: 26623, count: 1)--> (num: 27647, count: 1)-->
请按任意键继续. . .