寒假 5

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

复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef int datatype;
//定义节点结构体
typedef struct Node
{
	//数据域
	datatype data;
	//指针域:指向下一个节点的地址
	struct Node *next;
}*node;
//计算最大质数
int prime(int m)
{
	for(int i=m;i>2;i--)
	{
		int flag=0;
		for(int j=2;j<=sqrt(i);j++)
		{
			if(i%j==0)
			{
				flag=-1;
				break;
			}
		}
		if(flag==0)
			return i;
	}
}
//创建新节点
node create_node()
{
	node s=(node)malloc(sizeof(struct Node));
	if(s==NULL)
		return NULL;
	s->data=0;
	s->next=NULL;
	return s;
}
//插入
void insert_hash(datatype key,int p,node hash[])
{
	int index=key%p;
	hash[index];
	node s=create_node();
	s->data=key;
	if(hash[index]==NULL)
		hash[index]=s;
	else
	{
		s->next=hash[index];
		hash[index]=s;
	}
}
//输出
void output(node hash[],int m)
{
	for(int i=0;i<m;i++)
	{
		printf("%d:",i);
		printf("  NULL  ");
		node p=hash[i];
		while(p!=NULL)
		{
			printf("%-5d",p->data);
			p=p->next;
		}
		puts("");
	}
}
//查找
int search_hash(datatype key,int p,node hash[])
{
	int index=key%p;
	node head=hash[index];
	while(head!=NULL)
	{
		if(head->data==key)
			return 0;
		head=head->next;
	}
	return -1;
}
int main(int argc, const char *argv[])
{
	int arr[]={25,51,8,22,26,67,11,16,54,41};
	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;
	}
	//计算p(不大于哈希表长度的最大质数)
	int p=prime(m);
	//把数组元素存放到哈希表中
	for(int i=0;i<len;i++)
	{
		//把arr[i]元素插入到哈希表
		insert_hash(arr[i],p,hash);
	}
	//输出
	output(hash,m);
	//查找
	datatype key;
	printf("please enter search key:");
	scanf("%d",&key);
	int flag=search_hash(key,p,hash);
	if(flag==0)
		puts("exists");
	else
		puts("unexists");	
	return 0;
}

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)
{
		if(low>high)
			return -1;
		int mid=(low+high)/2;
		if(*(arr+mid)<key)
		{
			return half(key,arr,mid+1,high);
		}
		else if(*(arr+mid)>key)
		{

			return half(key,arr,low,mid-1);
		}
		else if(*(arr+mid)==key)
		{
			return mid;
		}
		else 
			return -1;
}
int main(int argc, const char *argv[])
{
	int arr[]={12,13,34,45,66,78,90};
	int key;
	printf("please enter key:");
	scanf("%d",&key);
	int len=sizeof(arr)/sizeof(arr[0]);
	int index=half(key,arr,0,len-1);
	if(index==-1)
		puts("unexists");
	else
		printf("index is %d\n",index);
	return 0;
}
相关推荐
tobias.b5 小时前
408真题解析-2010-7-数据结构-无向连通图
数据结构·算法·图论·计算机考研·408真题解析
良木生香6 小时前
【鼠鼠优选算法-双指针】003:快乐数 & 004:盛水最多的容器
算法
Cx330❀6 小时前
【优选算法必刷100题】第41-42题(模拟):Z 字形变换,外观数列
c++·算法
沃尔特。6 小时前
直流无刷电机FOC控制算法
c语言·stm32·嵌入式硬件·算法
CW32生态社区6 小时前
CW32L012的PID温度控制——算法基础
单片机·嵌入式硬件·算法·pid·cw32
Cx330❀6 小时前
【优选算法必刷100题】第038题(位运算):消失的两个数字
开发语言·c++·算法·leetcode·面试
漫随流水6 小时前
leetcode回溯算法(93.复原IP地址)
数据结构·算法·leetcode·回溯算法
燃于AC之乐6 小时前
我的算法修炼之路--5——专破“思维陷阱”,那些让你拍案叫绝的非常规秒解
c++·算法·贪心算法·bfs·二分答案·扩展域并查集·动态规划(最长上升子序列)
艾莉丝努力练剑6 小时前
【优选算法必刷100题】第021~22题(二分查找算法):山脉数组的峰顶索引、寻找峰值
数据结构·c++·算法·leetcode·stl
艾莉丝努力练剑6 小时前
【优选算法必刷100题】第007~008题(双指针算法):三数之和、四数之和问题求解
linux·算法·双指针·优选算法