📄 sanlielalianfa.cpp
字号:
// sanlielalianfa.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 10
struct list
{
int key;
struct list* next;
};
typedef struct list node;
typedef node* link;
node hashtable[MAX];
int hashfun(int value)
{
return value%MAX;
}
int findhash(int key)
{
int pos;
link ptr;
pos=hashfun(key);
ptr=hashtable[pos].next;
while(ptr!=NULL)
if(ptr->key==key)return 1;
else ptr=ptr->next;
return 0;
}
void createtable(int key)
{
link ptr;
link newnode;
int pos;
newnode=(link)malloc(sizeof(node));
newnode->key=key;
newnode->next=NULL;
pos=hashfun(key);
ptr=hashtable[pos].next;
if (ptr!=NULL)
{
newnode->next=ptr;
hashtable[pos].next=newnode;
}
else
hashtable[pos].next=newnode;
}
int main(int argc, char* argv[])
{ int checked[50];
int temp;
long temptime;
link ptr;
for (int i=0;i<MAX;i++)hashtable[i].next=NULL;
for(i=0;i<50;i++)checked[i]=0;
srand(time(&temptime)%60);/*使用时间初始随机数*/
i=0;
while (i!=10)
{
temp=rand()%50;
if (checked[temp]==0)
{
createtable(temp);
checked[temp]=1;
i++;
}
}
printf("散列表内容:\n");
for (i=0;i<MAX;i++)
{
printf("\n%2d",i);
ptr=hashtable[i].next;
while (ptr!=NULL)
{
printf("==>%d",ptr->key);
ptr=ptr->next;
}
}
while (1)
{
printf("\n请输入查找值(0~49)==>");
scanf("%d",&temp);
if (temp!=-1)
{
i=findhash(temp);
if(i!=0)
printf("找到查找值:%d\n",temp);
else
printf("没有找到查找值:%d\n",temp);
}
else
exit(1);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -