哈希存储、二分法查找

2024年2月5日
1.请编程实现哈希表的创建存储数组{12,24,234,234,23,234,23},输入key查找的值,实现查找功能

头文件:

#ifndef __HEAD_H__
#define __HEAD_H__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef int datatype;
typedef struct Node
{
	datatype data;
	struct Node *next;
}*node;
enum{FALSE=-1,SUCCESS};
int prime(int m);
void insert_hash(datatype element,int p,node hash[]);
void output(node hash[],int m);
int search_hash(datatype key,int p,node hash[]);


#endif

主函数:

#include"head.h"
int main(int argc, const char *argv[])
{
	int arr[]={12,24,234,234,23,234,23};
	int len=sizeof(arr)/sizeof(arr[0]);
	int m=len*4/3;
	node hash[m];
	for(int i=0;i<m;i++)
		hash[i]=NULL;
	int p=prime(m);
	for(int i=0;i<m;i++)
		insert_hash(arr[i],p,hash);
	hash[0]=NULL;
	output(hash,m);
	int key;
	printf("please enter key:");
	scanf("%d",&key);
	int flag=search_hash(key,p,hash);
	if(flag==0)
		puts("exists");
	else
		puts("unexists");
	return 0;
}

自定义函数:

#include"head.h"
/*
 * function:    计算最大质数
 * @param [ in] m
 * @param [out] p
 * @return      
 */
int prime(int m)
{
	for(int i=m;i>1;i--)
	{
		int flag=0;
		for(int j=2;j<i;j++)
		{
			if(i%j==0)
			{
				flag=-1;
				break;
			}
		}
		if(flag==0)
			return i;
	}
}
/*
 * function:    创建新节点
 * @param [ in] 
 * @param [out] 成功返回首地址,失败返回空
 * @return      
 */
node create()
{
	node s=(node)malloc(sizeof(struct Node));
	if(s==NULL)
		return NULL;
	s->data=0;
	s->next=NULL;
	return s;
}
/*
 * function:    哈希表插入
 * @param [ in] 插入值 p(最大质数) 表头 
 * @param [out] 
 * @return      
 */
void insert_hash(datatype element,int p,node hash[])
{
	node s=create();
	s->data=element;
	int index=element%p;
	if(hash[index]==NULL)
		hash[index]=s;
	else
	{
		s->next=hash[index];
		hash[index]=s;
	}
}
/*
 * function:    遍历输出
 * @param [ in] 哈希表 m
 * @param [out] 
 * @return      
 */
void output(node hash[],int m)
{
	for(int i=0;i<m;i++)
	{
		node p=hash[i];
		while(p)
		{
			printf("%-5d",p->data);
			p=p->next;
		}
		puts("NULL");
	}
}
/*
 * function:    查找
 * @param [ in] key p 哈希表
 * @param [out] 
 * @return      
 */
int search_hash(datatype key,int p,node hash[])
{
	int index=key%p;
	node q=hash[index];
	int i=0;
	while(q)
	{
		i++;
		if(q->data==key)
		{
			printf("row:%d line:%d\n",index+1,i);
			return SUCCESS;
		}
		q=q->next;
	}
	return FALSE;
}

2.现有数组{12,23,45,56,445,5676,6888],请输入key实现二分查找

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int half(int key,int arr[],int low,int high)
{
	while(low<=high)
	{
		int m=(low+high)/2;
		if(arr[m]<key)
			low=m+1;
		else if(arr[m]>key)
			high=m-1;
		else
			return m;
	}
}
int main(int argc, const char *argv[])
{
	int arr[]={12,23,45,56,445,5676,6888};
	int len=sizeof(arr)/sizeof(arr[0]);
	int key;
	printf("please enter key:");
	scanf("%d",&key);
	int index=half(key,arr,0,len-1);
	if(index<0 || index>len-1)
		puts("unexists");
	else
		printf("exists is arr[%d]\n",index);
	return 0;
}

  • 16
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值