数据结构day7(2023.7.21)

一、Xmind整理:

 

二、课上练习:

练习1:折半查找/二分查找

1-------100   key=88
   50---100
      75-100
    int arr[]={12,23,33,45,66,78,99};
    
    key=79
    12,23,33,45,66,78,99
    0                 6
    low      mid      high
                66,  78,  99
                mid+1 mid high
                low
                          99
                          low
                          high
                          mid
//查找:low<high
//low==high

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int half(int key,int *p,int low,int high)
{
	while(low<=high)
	{
		int mid=(low+high)/2;
		if(key==*(p+mid))
			return mid;
  	//降序
  	//else if(key<*(p+mid))
  	//升序	
  	else if(key>*(p+mid))
			low=mid+1;
		else
		high=mid-1;
	}
	return -1;
}
int main(int argc, const char *argv[])
{
	int high,n,key;
	printf("请问你要输入几组数据:");
	scanf("%d",&high);
	int arr[high];
	for(int n=0;n<high;n++)
	{
		printf("请输入第%d个数字:",n+1);
		scanf("%d",&arr[n]);
	}
	int low=0;
	printf("请输入关键词key:");
	scanf("%d",&key);
	int sub=half(key,arr,low,high);
	if(sub==-1)
		printf("key不存在!\n");
	else
		printf("find key in %d\n",sub);

	return 0;
}

练习2:哈希创建

/*
 * function:    创建节点
 * @param [ in] 
 * @param [out] 
 * @return      
 */
node create_node()
{
	node s=(node)malloc(sizeof(struct Node));
	if(NULL==s)
		return NULL;
	s->data=0;
	s->next=NULL;
	return s;
}

练习3:哈希释放空间

/*
 * function:    哈希表的释放
 * @param [ in] 
 * @param [out] 
 * @return      
 */
node free_space(node hash[],int p)
{
	for(int i=0;i<p;i++)
	{
		while(hash[i]!=NULL)
		{
			node q=hash[i];
			hash[i]=hash[i]->next;
			free(q);
			q=NULL;
		}
	}
}

哈希表的整体程序: 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
//哈希表是借助单链表实现的
typedef int datatype;
typedef struct Node
{
	//数据域
	datatype data;
	//指针域
	struct Node *next;
}*node;
/*
 * function:    计算最大质数
 * @param [ in] 
 * @param [out] 
 * @return      返回质数
 */
int max_prime(int m)
{
	for(int i=m;i>=2;i--)
	{
		int count=0;
		//i:m--2
		//判断i是否是质数,如果是则返回,不是则继续
		for(int j=2;j<=sqrt(i);j++)
		{
			if(i%j==0)
			{
				count++; //计算2-sqrt(i)之间约数的个数
				break;
			}
		}
		if(count==0)
			return i;
	}
}
/*
 * function:    创建节点
 * @param [ in] 
 * @param [out] 
 * @return      
 */
node create_node()
{
	node s=(node)malloc(sizeof(struct Node));
	if(NULL==s)
		return NULL;
	s->data=0;
	s->next=NULL;
	return s;
}

/*
 * function:    哈希表的插入
 * @param [ in] 
 * @param [out] 
 * @return      
 */
node insert_hash(int key,int p,node hash[])
{
	int sub=key%p;
	node L=hash[sub];
	//创建新节点s
	node s=create_node();
	s->data=key;
	if(L!=NULL)
	{
		s->next=L;
	}
	L=s;
	return L;
}
/*
 * function:    哈希表输出
 * @param [ in] 
 * @param [out] 
 * @return      
 */
void output(node hash[],int p)
{
	for(int i=0;i<p;i++)
	{
		printf("%d:",i);
		node L=hash[i];
		if(L==NULL)
		{
			puts("NULL");
			continue;
		}
		while(L!=NULL)
		{
			printf("%d\t",L->data);
			L=L->next;
		}
		puts("NULL");
	}
}
/*
 * function:    哈希表查找
 * @param [ in] 
 * @param [out] 
 * @return      查找成功返回0 失败返回-1
 */
int search_hash(datatype key,int p,node hash[])
{
	int sub=key%p;
	if(hash[sub]==NULL)
		return -1;
	node L=hash[sub];
	while(L!=NULL)
	{
		if(L->data==key)
		{
			return 0;
		}
		L=L->next;
	}
	return -1;
}
/*
 * function:    哈希表的释放
 * @param [ in] 
 * @param [out] 
 * @return      
 */
node free_space(node hash[],int p)
{
	for(int i=0;i<p;i++)
	{
		while(hash[i]!=NULL)
		{
			node q=hash[i];
			hash[i]=hash[i]->next;
			free(q);
			q=NULL;
		}
	}
}

int main(int argc, const char *argv[])
{
	//把数组的元素存到哈希表中,再通哈希表实现查找
	int arr[]={67,54,41,67,1093,2345,2345,123,34,123};
	int len=sizeof(arr)/sizeof(arr[0]);
	int m=len*4/3;
	int p=max_prime(m);
	//构建哈希表
	node hash[p];  //指针数组,存储p个指针
	for(int i=0;i<p;i++)
	{
		hash[i]=NULL;
	}
	//把数组元素存到哈希表中
	//循环数组元素存到哈希表
	for(int i=0;i<len;i++)
	{
		hash[arr[i]%p]=insert_hash(arr[i],p,hash);
	}
	//输出哈希表
	output(hash,p);

	//查找
	datatype key;
	printf("please enter find data:");
	scanf("%d",&key);
	int flag=search_hash(key,p,hash);
	if(flag==0)
		puts("success");
	else
		puts("error");

	//释放
	free_space(hash,p);
	output(hash,p);

	return 0;
}

 

练习4:递归循环5次

练习5:计算阶乘n!   (5!=1*2*3*4*5=5*4*3*2*1)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int rec(int n)
{
	if(n==0)
		return 1;
	else
	{
		return n*rec(n-1);
	}
}
int main(int argc, const char *argv[])
{
	int n;
	printf("please enter n:");
	scanf("%d",&n);
    int p=rec(n);
	printf("p=%d\n",p);
	return 0;
}

练习6:递归实现斐波那契     1 1 2 3 5 8 13.....

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int Fibonacci(int n)
{
	if(n==1||n==2)
		return 1;
	else
		return Fibonacci(n-1)+Fibonacci(n-2);
}
int main(int argc, const char *argv[])
{
	int n;
	printf("please enter n:");
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		Fibonacci(i);
		printf("%d\t",Fibonacci(i));
	}
   	printf("\n");

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值