数据结构与算法(六)查找 c语言实现

数据结构与算法(六)查找

  1. 静态查找表
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

#define MAX 20

typedef struct 
{
	int key;
}ElemType;

typedef struct
{
	ElemType r[MAX];
	int length;
}STable;

STable* initList(int n)
{
	STable* st = (STable*)malloc(sizeof(STable));
	if (st == NULL)
	{
		exit(-1);
	}
	for (int i = 0; i <= n; ++i)
	{
		st->r[i].key = 0;
	}
	st->length = n;
	return st;//返回指针变量一定要写,如果只在初始化函数里面初始化,出了这个函数指针就已经不存在了,
	          //会报没有初始化局部变量的问题
}

void CreateList(STable* st, int n)
{
	int i;
	for (i = 1; i <= n; ++i)
	{
		printf("请输入第%d个元素:",i);
		scanf_s("%d",&(st->r[i].key));
	}
}

int SeqSearch(STable* st, int key)//顺序查找
{
	//int i;要比较两次,需要改进
	//for (i = st.length; st.r[i].key != key && i >= 1; --i);
	//if (i > 0)
	//	return i;
	//else
	//	return 0;
	int i;
	st->r[0].key = key;
	for (i = st->length; st->r[i].key != key; --i);
	return i;
}

int BinSearch(STable* st, int key)//二分查找循环版
{
	int low = 1;
	int high = st->length;
	while (low <= high)
	{
		int mid = (low + high) / 2;
		if (st->r[mid].key == key)
			return mid;
		else if (key < st->r[mid].key)
			high = mid - 1;
		else
			low = mid + 1;
	}
	return 0;
}

int Bin_Search(STable* st, int low, int high,int key)//二分查找递归版
{
	int mid;
	if (low > high)
		return 0;
	mid = (low + high) / 2;
	if (key == st->r[mid].key)
		return mid;
	else if (key < st->r[mid].key)
	{
		high = mid - 1;
		Bin_Search(st,low,high,key);
	}
	else
	{
		low = mid + 1;
		Bin_Search(st, low, high, key);
	}
}

int main()
{
	int n;
	printf("请输入元素个数:");
	scanf_s("%d", &n);
	STable* st= initList(n);
	CreateList(st,n);
	//printf("用顺序查找法查找的结果是:%d\n", SeqSearch(st, 2));
	printf("用折半查找法查找的结果是:%d\n", BinSearch(st, 6));
	printf("用折半查找法(递归)查找的结果是:%d\n", Bin_Search(st, 1, n, 6));
	return 0;
}

在这里插入图片描述

  1. 动态查找表
/*二叉排序树的实现:左小右大*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

typedef struct
{
	int key;
	int otherKey;
}ElemType;

typedef struct BSTNode
{
	ElemType data;
	struct BSTNode* lchild, * rchild;
}BSTNode,*BSTree;

BSTree* initTree(int n) //补充图里面所说的用数组创造图并且释放内存
{
	int i;
	BSTree* node = (BSTree*)malloc(sizeof(BSTree)*n);
	for (i = 0; i < n; ++i)
	{
		node[i] = (BSTree)malloc(sizeof(BSTNode));
		if (node[i] == NULL)
		{
			exit(-1);
		}
		printf("请输入第%d个节点的元素: ",i+1);
		scanf_s(" %d",&(node[i]->data.key));
	}
	node[4]->lchild = node[1];//此处根据图的情况写
	node[4]->rchild = node[6];
	node[1]->lchild = node[0];
	node[1]->rchild = node[3];
	node[0]->lchild = NULL;
	node[0]->rchild = NULL;
	node[3]->lchild = node[2];
	node[3]->rchild = NULL;
	node[6]->lchild = node[5];
	node[6]->rchild = node[8];
	node[5]->lchild = NULL;
	node[5]->rchild = NULL;
	node[8]->lchild = node[7];
	node[8]->rchild = NULL;
	node[2]->lchild = NULL;
	node[2]->rchild = NULL;
	node[7]->lchild = NULL;
	node[7]->rchild = NULL;
	return node;
}

void InsertBST(BSTree* bs, int key)
{
	BSTree s;
	if (*bs == NULL)
	{
		s = (BSTree)malloc(sizeof(BSTNode));
		s->data.key = key;
		s->lchild = NULL;
		s->rchild = NULL;
		*bs = s;
	}
	else
	{
		if (key < (*bs)->data.key)
			InsertBST(&(*bs)->lchild,key);
		else if(key > (*bs)->data.key)
			InsertBST(&(*bs)->rchild, key);
	}
}


void CreateBST(BSTree* bs)
{
	int key;
	*bs = NULL;
	printf("请输入元素: ");
	scanf_s("%d",&key);
	while (key != -1)//记得输入-1用来结束
	{
		InsertBST(bs,key);
		printf("请输入元素: ");//输入:6 15 39 58 67 76 80 88 97进行验证
		scanf_s("%d",&key);
	}
}

void InOrder(BSTree bs)
{
	if (bs == NULL)
		return;
	InOrder(bs->lchild);
	printf("%d\t",bs->data.key);
	InOrder(bs->rchild);
}

BSTree SearchBST(BSTree bs, int key)
{
	if (!bs || bs->data.key == key)
		return bs;
	else if (key < bs->data.key)
		return SearchBST(bs->lchild, key);
	else
		return SearchBST(bs->rchild,key);
}

BSTree DelBST(BSTree bs, int key)
{
	BSTree p, f, s, q;
	p = bs;
	f = NULL;
	while (p)
	{
		if (p->data.key == key)
			break;
		f = p;
		if (p->data.key > key)
			p = p->lchild;
		else
			p = p->rchild;
	};
	if (p == NULL)
		return bs;
	if (p->lchild && p->rchild)
	{
		q = p;
		s = p->lchild;
		while (s->rchild)
		{
			q = s;
			s = s->rchild;
		}
		p->data.key = s->data.key;
		if (q != p)
			q->lchild = s->rchild;
		else
			q->lchild = s->lchild;
		free(s);
	}
	else
	{
		if (!p->rchild)
		{
			q = p;
			p = p->lchild;
		}
		else
		{
			q = p;
			p = p->rchild;
		}
		if (!f)
			bs = p;
		else
			if (q == f->lchild)
				f->lchild = p;
			else
				f->rchild = p;
		free(q);
	}
	return bs;
}

int main()
{
	/*BSTree* node = initTree(9);//此段是适应于数组建立的图的方式
	InOrder(node[4]);
	printf("\n");
	if (!SearchBST(node[4],6))
		printf("找不到\n");
	else
		printf("能找到\n");
	free(node);
	node = NULL;*/


	BSTree bs;
	CreateBST(&bs);
	InOrder(bs);
	printf("\n");
	InOrder(DelBST(bs, 15));//删除操作
	printf("\n");
	return 0;
}

在这里插入图片描述

  1. 哈希表
/*拉链法比较实用*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

#define MAX 6

typedef struct node
{
	int data;
	struct node* next;
}NODE,*PNODE;

typedef struct elem
{
	int empty;
	int data;
	PNODE Nextelem;
}ElemType;

ElemType hs[MAX];

void initHash(ElemType* hs)
{
	for (int i = 0; i < MAX; i++)
	{
		hs[i].empty = 1;
		hs[i].data = NULL;
		hs[i].Nextelem = NULL;
	}
}

int H(int key)
{
	return key % 2;
}

bool CreateHash()
{
	int i = 0;
	int n,m,num;
	printf("您要输入的数字数: ");
	scanf_s("%d",&n);
	while (i < n)
	{
		printf("请您输入第%d个数字:",i+1);
		scanf_s("%d",&num);
		m = H(num);
		if (hs[m].empty == 1)
		{
			hs[m].data = num;
			hs[m].empty = 0;
		}
		if(hs[m].empty != 1)
		{
			PNODE pNew = (PNODE)malloc(sizeof(NODE));
			if (pNew == NULL)
				exit(-1);
			pNew->data = num;
			pNew->next = hs[m].Nextelem;
			hs[m].Nextelem = pNew;
		}
		i = i + 1;
	}
}

void Search(int key)
{
	int i = H(key);
	PNODE p;
	if (hs[i].data == key)
		printf("能找到,在第%d项表上\n", i + 1);
	else
	{
		p = hs[i].Nextelem;
		while (p!= NULL)
		{
			if (p->data == key)
			{
				printf("能找到,在第%d项表的链子上\n",i+1);
				break;
			}
			if(p->data!=key)
			{
				p = p->next;
			}
		}
		if(p==NULL)
			printf("找不到\n");
	}
	return;
}

int main()
{
	initHash(hs);
	CreateHash();
	Search(2);
	Search(4);
	Search(3);
}

在这里插入图片描述

16进制10进制.txt 32.txt asm.txt Crctable.txt C标志符命名源程序.txt erre.txt erre2.txt ff.txt for循环的.txt list.log N皇后问题回溯算法.txt ping.txt re.txt source.txt winsock2.txt ww.txt 万年历.txt 万年历的算法 .txt 乘方函数桃子猴.txt 乘法矩阵.txt 二分查找1.txt 二分查找2.txt 二叉排序树.txt 二叉树.txt 二叉树实例.txt 二进制数.txt 二进制数2.txt 余弦曲线.txt 余弦直线.txt 傻瓜递归.txt 冒泡排序.txt 冒泡法改进.txt 动态计算网络最长最短路线.txt 十五人排序.txt 单循环链表.txt 单词倒转.txt 单链表.txt 单链表1.txt 单链表2.txt 单链表倒序.txt 单链表的处理全.txt 双链表正排序.txt 反出字符.txt 叠代整除.txt 各种排序法.txt 哈夫曼算法.txt 哈慢树.txt 四分砝码.txt 四塔1.txt 四塔2.txt 回文.txt 图.txt 圆周率.txt 多位阶乘.txt 多位阶乘2.txt 大加数.txt 大小倍约.txt 大整数.txt 字符串查找.txt 字符编辑.txt 字符编辑技术(插入和删除) .txt 完数.txt 定长串.txt 实例1.txt 实例2.txt 实例3.txt 小写数字转换成大写数字1.txt 小写数字转换成大写数字2.txt 小写数字转换成大写数字3.txt 小字库DIY-.txt 小字库DIY.txt 小孩分糖果.txt 小明买书.txt 小白鼠钻迷宫.txt 带头结点双链循环线性表.txt 平方根.txt 建树和遍历.txt 建立链表1.txt 扫描码.txt 挽救软盘.txt 换位递归.txt 排序法.txt 推箱子.txt 数字移动.txt 数据结构.txt 数据结构2.txt 数据结构3.txt 数组完全单元.txt 数组操作.txt 数组递归退出.txt 数组递归退出2.txt 文件加密.txt 文件复制.txt 文件连接.txt 无向图.txt 时间陷阱.txt 杨辉三角形.txt 栈单元加.txt 栈操作.txt 桃子猴.txt 桶排序.txt 检出错误.txt 检测鼠标.txt 汉字字模.txt 汉诺塔.txt 汉诺塔2.txt 灯塔问题.txt 猴子和桃.txt 百鸡百钱.txt 矩阵乘法动态规划.txt 矩阵转换.txt 硬币分法.txt 神经元模型.txt 穷举搜索法.txt 符号图形.txt 简单数据库.txt 简单计算器.txt 简单逆阵.txt 线性顺序存储结构.txt 线索化二叉树.txt 绘制圆.txt 编随机数.txt 网络最短路径Dijkstra算法.txt 自我复制.txt 节点.txt 苹果分法.txt 螺旋数组1.txt 螺旋数组2.txt 试题.txt 诺汉塔画图版.txt 读写文本文件.txt 货郎担分枝限界图形演示.txt 货郎担限界算法.txt 质因子.txt 输出自已.txt 迷宫.txt 迷宫问题.txt 逆波兰计算器.txt 逆矩阵.txt 逆阵.txt 递堆法.txt 递归桃猴.txt 递归车厢.txt 递推.txt 逻辑移动.txt 链串.txt 链栈.txt 链表十五人排序.txt 链表(递归).txt 链队列.txt 队列.txt 阶乘递归.txt 阿姆斯特朗数.txt 非递归.txt 顺序栈.txt 顺序表.txt 顺序队列.txt 骑士遍历1.txt 骑士遍历2.txt 骑士遍历回逆.txt 黑白.txt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值