7.25数据结构相关练习

关于哈希表、哈希查找与快速排序练习

1.哈希查找练习

 初始化哈希表

void init_hash(Node *hash[])
{
	for(int i=0;i<P;i++)
	{
		hash[i]=NULL;
	}
	printf("初始化成功\n");
}

插入哈希表

int insert_hash(Node*hash[],int x)
{
	int index=x%P;
	//将数据封装成节点
	Node *q=(Node*)malloc(sizeof(Node));
	if (NULL==q)
	{
		return -1;
	}
	q->data=x;
	q->next=NULL;
	//使用头插法将结点放入链表
	q->next=hash[index];
	hash[index]=q;
	printf("");
	return 0;


}

查看哈希表

void show(Node*hash[])
{
	int i;
	for(i=0;i<P;i++)
	{
		printf("%d:  ",i+1);
		Node*q=hash[i];
		while(q!=NULL)
		{
			printf("%d->",q->data);
			q=q->next;
		}
		printf("NULL\n");
	}
}

在哈希表中查找内容


void search_hash(Node*hash[],int key)
{
	int index=key%P;
	Node * q=hash[index];
	while(q!=NULL&&q->data!=key)
	{
		q=q->next;
	}
	if (NULL==q)
	{
		printf("没找到\n");
	}else
	{
		printf("在表中存在\n");
	}
}

在mian中进行初始话并测试代

#include<stdlib.h>
#include"hash.h"

#include <stdio.h>
int main(int argc, const char *argv[])
{
	int arr[N]={25,51,8,22,26,67,11,16,54,41};
	Node *hash[P];
	init_hash(hash);
	for(int i=0;i<N;i++)
	{
		insert_hash(hash,arr[i]);
	}
	show(hash);
	int se;
	printf("希望查找:");
	scanf("%d",&se);
	search_hash(hash,se);



	return 0;
}

 

2.快速排序练习

int part ( int *arr,int low, int high)
{
	int x=arr[low];
	while(low<high)
	{
		while(arr[high]>=x &&low<high)
		{
			high--;
		
		}
		arr[low] =arr[high];
		while(arr[low]<=x &&low<high)
		{
			low++;
		
		}
		arr[high]=arr[low];
	}
	arr[low]=x;
	return low;



}
void quick (int*arr,int low,int high)  //快速排序
{
	if (low<high)
	{
		int mid = part(arr,low,high);
		quick(arr,low,mid-1);
		quick(arr,mid+1,high);
	}
}

在主函数中对数组进行赋值在调用函数测试排序

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值