散列数据结构代码(线性探测法和拉链法)

线性探测法

  1 //使用线性探测法和数组结构创建散列表,
  2 //然后输入数据的查询。并且将创建过程
  3 //都输出来
  4 #include <stdlib.h>
  5 #include <stdio.h>
  6 #include <time.h>
  7 #define MAX 10      //最大数组容量
  8 #define NUM 8       //随机数生成的个数
  9 #define BLANK -1    //空白空间
 10 
 11 int hashtable[MAX];     //散列表声明
 12 
 13 //散列函数
 14 int hashfunc(int value)
 15 {
 16     return value % MAX;     //余数
 17 }
 18 
 19 //线性探测法
 20 int linehash(int key)
 21 {
 22     int pos;        //位置变量
 23     int temp;
 24     
 25     pos = hashfunc(key);
 26     temp = pos;         //保留开始位置
 27     while(hashtable[temp] != key &&        //线性探测循环
 28      hashtable[temp] != BLANK)    
 29     {
 30         //使用余数将数组变为环状
 31         temp = (temp + 1) % MAX;    //下一个位置
 32         if(pos == temp)             //查询结束
 33             return -1;              //没有找到
 34     }
 35     if(hashtable[temp] == BLANK)    //是否空白
 36         return -1;
 37     else
 38         return temp;                //找到了
 39 }
 40 
 41 
 42 //创建散列表
 43 void createtable(int key)
 44 {
 45     int pos;                        //位置变量
 46     int temp;
 47     //调用散列函数计算位置
 48     pos = hashfunc(key);
 49     temp = pos;                         //保留开始位置
 50     while(hashtable[temp] != BLANK) //找寻存入位置
 51     {
 52         temp = (temp + 1) % MAX;    //下一个位置
 53         if(pos == temp)             //散列表是否已满
 54         {
 55             printf("散列表已满!\n");
 56             return;
 57         }
 58     }
 59     hashtable[temp] = key;         //存入散列表
 60 }
 61 
 62 
 63 //主程序:散列查找法
 64 int main()
 65 {
 66     int checked[50];                //检查数组
 67     int i,j,temp;
 68     long temptime;
 69     for(i = 0;i < MAX;i++)
 70         hashtable[i] = BLANK;       //清除散列表
 71     for(i = 0;i < 50;i++)
 72         checked[i] = 0;                //清楚检查数组
 73         
 74     printf("散列表内容:\n");
 75     srand(time(&temptime) % 60);    //使用时间初始随机数
 76     i = 0;
 77     while(i != NUM)                 //生成数组值的循环
 78     {
 79         temp = rand() % 50;         //随机数取值 0-49
 80         if(checked[temp] == 0)      //是否是已有的值
 81         {
 82             createtable(temp);      //创建散列表
 83             printf("%2d => ",temp);
 84             for(j = 0;j < MAX;j++)  //输出散列表
 85                 printf("[%2d]",hashtable[j]);   
 86             printf("\n");
 87             checked[temp] = 1;      //设置此值生成过
 88             i++;                    //下一个数组值
 89         }
 90     }
 91     
 92     while(1)                        //主循环开始
 93     {
 94         printf("\n请输入查找值(0-49) ==> ");
 95         scanf("%d",&temp);          //导入查找值
 96         if(temp != -1)
 97         {
 98             i = linehash(temp);     //调用散列查找
 99             if(i != -1)
100                 printf("找到查找值:%d[%d]\n",temp,i);
101             else
102                 printf("没有找到查找值:%d\n",temp);
103         }
104         else
105             exit(1);                //结束程序
106     }
107     return 0;
108 }

用链表创建散列表

  1 //用链表创建散列表
  2 #include <stdlib.h>
  3 #include <time.h>
  4 #include <stdio.h>
  5 #define MAX 10
  6 #define NUM 10
  7 
  8 struct list
  9 {
 10     int key;
 11     struct list*next;
 12 };
 13 typedef struct list node;
 14 typedef node* link;
 15 
 16 node hashtable[MAX];
 17 
 18 int hashfunc(int value)
 19 {
 20     return value % MAX;
 21 }
 22 
 23 int linkhash(int key)
 24 {
 25     link ptr;
 26     int pos;
 27     
 28     pos = hashfunc(key);
 29     ptr = hashtable[pos].next;
 30     while(ptr != NULL)
 31         if(ptr->key == key)
 32             return 1;
 33         else
 34             ptr = ptr->next;
 35     return 0;
 36 }
 37 
 38 void createtable(int key)
 39 {
 40     link ptr;
 41     link new;
 42     int pos;
 43     
 44     new = (link)malloc(sizeof(node));
 45     new->key = key;
 46     new->next = NULL;
 47     
 48     pos = hashfunc(key);
 49     ptr = hashtable[pos].next;
 50     if(ptr != NULL)
 51     {
 52         new->next = ptr;
 53         hashtable[pos].next = new;
 54     }
 55     else
 56         hashtable[pos].next = new;
 57 }
 58 
 59 int main()
 60 {
 61     link ptr;
 62     int checked[50];
 63     int i,temp;
 64     long temptime;
 65     for(i = 0;i < MAX;i++)
 66         hashtable[i].next = NULL;
 67     for(i = 0;i < 50;i++)
 68         checked[i] = 0;
 69         
 70     srand(time(&temptime) % 60);
 71     i = 0;
 72     while(i != NUM)
 73     {
 74         temp = rand() % 50;
 75         if(checked[temp] == 0)
 76         {
 77             createtable(temp);
 78             checked[temp] = 1;
 79             i++;
 80         }
 81     }
 82     printf("散列表内容:\n");
 83     for(i = 0;i < MAX;i++)
 84     {
 85         printf("\n%2d ==> ",i);
 86         ptr = hashtable[i].next;
 87         while(ptr != NULL)
 88         {
 89             printf("[%2d]",ptr->key);
 90             ptr = ptr->next;
 91         }
 92     }
 93     
 94     while(1)
 95     {
 96         printf("\n请输入查找值(0-49) ==> ");
 97         scanf("%d",&temp);
 98         if(temp != -1)
 99         {
100             i = linkhash(temp);
101             if(i != 0)
102                 printf("找到查找值:%d\n",temp);
103             else
104                 printf("没有找到查找值:%d\n",temp);
105         }
106         else
107             exit(1);
108     }
109     return 0;
110 }

posted @ 2020-12-30 15:48  互联星空  阅读(541)  评论(0编辑  收藏  举报