C语言数据结构,查找的基本算法。

  • 设计思路:顺序查找:设置哨兵,让数组的首地址存查找的值,然后遍历,看其是否与首地址的值相等。折半查找:设置下限和上限,让上限和下限相除以2,等到的数值做判断,小于或大于要查找的值,若小于,缩进后半部分,留下前半部分继续查找,反之亦然。二叉排序树查找:建立二叉树,让小的数值放进左孩子,让大的数值放进又孩子。
  • typedef int type_hyb;
    //typedef int type_h
    typedef struct node {
    	//这是链表的模型
    	type_hyb *hyb;
    	type_hyb lenght;
    	//以下为树的结构体模型
    	type_hyb data;
    	struct node* left, * right;
    
    }node;
    //创建一堆数
    node* creat(type_hyb lenght) {
    	
    }
    //顺序查找
    type_hyb search_1(node* p,int x) {
    
    	
    }
    //折半查找
    type_hyb search_2(node* p,int x1) {
    }
    //创建二叉排序树
    node* search_3(node *tree,int key) {
    }
    //二叉排序树的查找
    node* search_4(node* tree,int key1) {	
    }
    
    ③ 完整代码:
    #include<stdio.h>
    #include<malloc.h>
    #include<stdlib.h>
    #include<time.h>
    #define ok 1
    #define error 0
    #define NULL 0
    typedef int type_hyb;
    //typedef int type_h
    typedef struct node {
    	//这是链表的模型
    	type_hyb *hyb;
    	type_hyb lenght;
    	//以下为树的结构体模型
    	type_hyb data;
    	struct node* left, * right;
    
    }node;
    //创建一堆数
    node* creat(type_hyb lenght) {
    	node *p = (node*)malloc(sizeof(node));
    	p->hyb =(type_hyb*)malloc(sizeof(type_hyb)*lenght);
    	p->lenght = lenght;
    	srand(time(NULL));
    	for (int i = 1; i <=lenght; i++) {
    		  p->hyb[i]=rand()%100;
    	}
    	return p;
    }
    //顺序查找
    type_hyb search_1(node* p,int x) {
    
    	p->hyb[0]=x;
    	int i;
    	for (i = p->lenght; p->hyb[i] != x; --i);
    	return i;
    }
    //折半查找
    type_hyb search_2(node* p,int x1) {
    	
    	int small, high,mid;
    	small = 1; high = p->lenght;
    	while (small <= high) {
    		mid = (small + high) / 2;
    		if (p->hyb[mid] == x1)
    			return mid;
    		else if (p->hyb[mid] > x1)
    			high = mid - 1;
    		else
    			small = mid + 1;
    	}
    	return 0;
    
    }
    //创建二叉排序树
    node* search_3(node *tree,int key) {
    	
    	
    	if (tree == NULL) {
    		node* t = (node*)malloc(sizeof(node));
    		tree = t;
    		tree->data = key;
    		tree->left = tree->right = NULL;
    		return tree;
    
    	}
    	else if(key==tree->data){
    		return error;
    	
    	}
    	else if (key<tree->data) {
    		tree->left=search_3(tree->left,key);
    		return tree;
    	
    	}
    	else {
    		tree->right=search_3(tree->left, key);
    		return tree;
    	}
    	return tree;
    }
    //二叉排序树的查找
    node* search_4(node* tree,int key1) {
    	
    	while (tree != NULL && key1 != tree->data) {
    		if (key1 < tree->data)
    		{
    			tree = tree->left;
    		}
    		else {
    			tree = tree->right;
    		}
    	}
    	return tree;
    	
    }
    
    
    
    void main() {
    	printf("0-创建一堆数:");
    	printf("1-顺序查找:\n");
    	printf("2-折半查找:\n");
    	printf("3-二叉排序树查找:\n");
    	node* p, *tree = new node;
    	tree = NULL;
    	p = NULL;
    	type_hyb lenght;
    	int week, y,u,mid1,key;
    	do {
    		printf("请你选择功能!\n");
    		int week;
    		scanf_s("%d", &week);
    		switch (week)
    		{
    		case 0:
    			printf("请输入你要创建的数组长度:");
    			scanf_s("%d",&lenght);
    			p = creat(lenght);
    			printf("创建随机数组成功!");
    			for (int i = 1; i <= lenght; i++)
    				printf("%d\n",p->hyb[i]);
    			printf("请选择是否执行功能:1or0\n");
    			scanf_s("%d", &y);
    			if (y == 1)
    			{
    
    				continue;
    			}
    			else
    				break;
    		case 1:
    			int x;
    			printf("请输入你要查找的值:");
    			scanf_s("%d", &x);
    			u = search_1(p,x);
    			printf("你的数值在第%d位", u);
    			printf("请选择是否执行功能:1or0\n");
    			scanf_s("%d", &y);
    			if (y == 1)
    			{
    
    				continue;
    			}
    			else
    				break;
    		case 2:
    			int x1;
    			printf("请输入你要查找的值:");
    			scanf_s("%d", &x1);
    			mid1 = search_2(p,x1);
    			printf("你的数值在第%d位", mid1);
    			printf("请选择是否执行功能:1or0\n");
    			scanf_s("%d", &y);
    			if (y == 1)
    			{
    				continue;
    			}
    			else
    				break;
    		case 3:
    			for (int i =1; i <= p->lenght; i++) {
    				key = p->hyb[i];
    				tree=search_3(tree, key);
    			}
    			int key1;
    			printf("请输入要你要查找的值:");
    			scanf_s("%d", &key1);
    			tree=search_4(tree,key1);
    			for (int i = 1; i <=p->lenght; i++) {
    				if (tree!=NULL&&tree->data ==p-> hyb[i]) {
    					printf("%d存在数组里的第%d位\n", tree->data, i);
    				}
    				else if (tree == NULL) {
    					printf("改值没找到!\n");
    					break;
    				}
    			}
    			printf("请选择是否执行功能:1or0\n");
    			scanf_s("%d", &y);
    			if (y == 1)
    			{
    				continue;
    			}
    			else
    				break;
    
    
    		}
    	} while (y == 1);
    	
    }

     

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值