7.25-数据结构-作业

第一题:

使用哈希存储将数据存入哈希表中,并进行查找 

1、已知一组关键字序列为(25,51,8,22,26,67,11,16,54,41)

2、其哈希地址空间为[0,...,2]

3、Hash函数定义为: H(key) = key MOD 13

4、使用链地址法处理冲突,画出对应的哈希表,并编码实现

主函数

#include "hash.h"

int main(int argc, const char *argv[])
{
	int arr[] = {25,51,8,22,26,67,11,16,54,41};
	Node *hash[M];
	init_hash(hash);
	for(int i = 0;i<N;i++)
	{
		insert_hash(hash,arr[i]);
	}
	show_hash(hash);
	printf("输入要查找的数>>>");
	int num;
	scanf("%d",&num);
	serach_hash(hash,num);
	return 0;
}

头文件

#ifndef __HASH_H__
#define __HASH_H__
#define N 10
#define M 13
#include <stdio.h>
#include <stdlib.h>
typedef int datatype;
typedef struct Node
{
	datatype data;
	struct Node *next;
}Node;

//创建
void init_hash(Node **hash);
//将元素存入哈希表函数
int insert_hash(Node **hash,int x);
//定义查看哈希表的函数
void show_hash(Node **hash);
//定义哈希查找函数
void serach_hash(Node **hash,int key);
#endif

功能函数

#include "hash.h"

//创建
void init_hash(Node **hash)
{
	for(int i=0;i<M;i++)
	{
		*(hash+i) = NULL;//指向NULL
	}
	printf("创建成功\n");
}
//将元素存入哈希表函数
int insert_hash(Node **hash,int x)
{
	int index = x%M;   //摸M,13
	Node *p = (Node*)malloc(sizeof(Node));//开辟链表堆空间
	p->data = x; //初始化
	p->next = *(hash+index);//头插
	*(hash+index) = p;
	return 0;
	
}
//定义查看哈希表的函数
void show_hash(Node **hash)
{
	for(int i=0;i<M;i++)//找到每一个哈希表元素
	{
		Node *q = *(hash+i);//指哈希表存储的地址
		printf("%d:",i);
		while(q!=NULL)//循环遍历链表到结束
		{
			printf("%d->",q->data);
			q = q->next;
		}
		printf("NULL\n");
	}
}

//定义哈希查找函数
void serach_hash(Node **hash,int key)
{
	int index = key%M;//查找的值%M
	Node *q = *(hash+index);//得到index,指针指向哈希表下标为index的地址
	while(q!=NULL && q->data!=key)//链表结束或者查找到值结束循环
	{
		q = q->next;
	}
	if(q==NULL)//链表遍历完没有找到
	{
		printf("没有找到%d\n",key);
	}
	else
	{
		printf("成功找到%d\n",key);
	}
}

第二题

使用冒泡排序、选择排序、插入排序、快速排序完成下面案例

主函数

#include "sort.h"

int main(int argc, const char *argv[])
{
	int arr[] = {198,289,98,357,85,170,232,110};
	int len = sizeof(arr)/sizeof(int);
	printf("原数组元素是\n");
	show(arr,len);
	printf("冒泡排序升序后的元素是\n");
	buble_sort(arr,len);
	show(arr,len);
	printf("选择排序降序后的元素是\n");
	selection_sort(arr,len);
	show(arr,len);
	printf("插入排序升序后的元素是\n");
	insert_sort(arr,len);
	show(arr,len);
	printf("快速排序降序后的元素是\n");
	quick_sort(arr,0,len-1);
	show(arr,len);

	return 0;
}

头文件

#ifndef __SORT_H__
#define __SORT_H__
#include <stdio.h>
//遍历
void show(int *arr,int n);
//冒泡排序升序
void buble_sort(int *arr,int n);
//选择排序降序
void selection_sort(int *arr,int n);
//插入排序升序
void insert_sort(int *arr,int n);
//快速排序降序
void quick_sort(int *arr,int low,int high);
int port(int *arr,int low,int high);

#endif

功能函数

#include "sort.h"
//遍历
void show(int *arr,int n)
{
	int i;
	for(i=0;i<n;i++)
	{
		printf("%d\t",arr[i]);
	}
	printf("\n");
}
//冒泡排序升序
void buble_sort(int *arr,int n)
{
	int i,j;
	int flag = 0;
	int temp;
	for(i=1;i<n;i++)
	{
		flag = 0;
		for(j=0;j<n-i;j++)
		{
			if(arr[j]>arr[j+1])
			{
				temp = arr[j];
				arr[j] = arr[j+1];
				arr[j+1] = temp;
				flag = 1;
			}
		}
		if(flag==0)
		{
			return;
		}
	}
}
//选择排序降序
void selection_sort(int *arr,int n)
{
	int i,j;
	int index;
	int temp;
	for(i=0;i<n;i++)
	{
		index = i;
		for(j=i+1;j<n;j++)
		{
			if(arr[j]>arr[index])
			{
				index = j;
			}
		}
		if(index!=i)
		{
			temp = arr[i];
			arr[i] = arr[index];
			arr[index] = temp;
		}
	}
}
//插入排序升序
void insert_sort(int *arr,int n)
{
	int i,j;
	int temp;
	for(i=1;i<n;i++)
	{
		temp = arr[i];
		for(j=i;j>0 && arr[j-1]>temp;j--)
		{
			arr[j] = arr[j-1];	
		}
		arr[j] = temp;
	}
}
//一趟快排
int port(int *arr,int low,int high)
{
	int x = arr[low];
	while(low<high)
	{
		while(low<high && arr[high]<x)
		{
			high--;
		}
		arr[low] = arr[high];
		while(low<high && arr[low]>x)
		{
			low++;
		}
		arr[high] = arr[low];
	}
	arr[low] = x;
	return low;
}

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


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值