查找--静态查找与动态查找

静态查找:

1.查询某个特定的数据元素是否在查找表中;

2.检索某个特定的数据元素的各种属性。



动态查找:

1.在查找表中插入一个元素;

2.从查找表中删去某个数据元素。

(需要借助于顺序表)

//SeqList.h

#ifndef _SEQLIST_H_
#define _SEQLIST_H_

typedef void SeqList;
typedef void SeqListNode;

SeqList* SeqList_Create(int capacity);

void SeqList_Destroy(SeqList* list);

int SeqList_Length(SeqList* list);

int SeqList_Capacity(SeqList* list);

void SeqList_Clear(SeqList* list);

int SeqList_Insert(SeqList* list,SeqListNode* node,int pos);

SeqListNode* SeqList_Get(SeqList* list,int pos);

SeqListNode* SeqList_Delete(SeqList* list,int pos);

#endif

//SeqList.c

#include "SeqList.h"
#include <malloc.h>

typedef unsigned int TSeqListNode;
typedef struct _tag_SeqList
{
	int capacity;
	int length;
	TSeqListNode* node;	
}TSeqList;

SeqList* SeqList_Create(int capacity)
{
	TSeqList* ret = NULL;
	if(capacity > 0)
	{
		ret = (TSeqList*)malloc(sizeof(TSeqList)+sizeof(TSeqListNode)*capacity);
	}
	if(ret != NULL)
	{
		ret->capacity = capacity;
		ret->length = 0;
		ret->node = (TSeqListNode*)(ret+1);	
	}
	return ret;
}

void SeqList_Destroy(SeqList* list)
{
	free(list);
}
int SeqList_Length(SeqList* list)
{
	int ret = -1;
	TSeqList* sList = (TSeqList*)list;
	if(sList!=NULL)
	{
		ret = sList->length;
	}
	return ret;
}
int SeqList_Capacity(SeqList* list)
{
	int ret = -1;
	TSeqList* sList = (TSeqList*)list;
	if(sList != NULL)
	{
		ret = sList->capacity;	
	}
	return ret;
}
void SeqList_Clear(SeqList* list)
{
	TSeqList* sList = (TSeqList*)list;
	if(sList != NULL)
	{
		sList->length = 0;
	}
}
int SeqList_Insert(SeqList* list,SeqListNode* node,int pos)
{
	int ret = 0;
	TSeqList* sList = (TSeqList*)list;
	ret = (sList != NULL)&&(node != NULL)&&(pos >= 0)&&(sList->length+1 <= sList->capacity);
	int i = 0;
	if( ret )
	{
		if(pos>sList->length)
		{
			pos = sList->length;	
		}
		for(i=sList->length;i>pos;i--)
		{
			sList->node[i] = sList->node[i-1];
		}
		sList->node[i] = (TSeqListNode)node;
		sList->length++;
	}
	return ret;
}

SeqListNode* SeqList_Get(SeqList* list,int pos)
{
	TSeqList* sList = (TSeqList*)list;
	TSeqListNode* ret = NULL;
	if((sList!=NULL)&&(pos>=0)&&(pos<sList->length))
	{
		ret = (TSeqListNode*)sList->node[pos];	
	}
	return ret;
}

SeqListNode* SeqList_Delete(SeqList* list,int pos)
{
	TSeqList* sList = (TSeqList*)list;
	TSeqListNode* ret = SeqList_Get(list,pos);
	int i = 0;
	if(ret != NULL)
	{
		for(i=pos;i<sList->length;i++)
		{
			sList->node[i] = sList->node[i+1];	
		}
		sList->length--;
	}
	return ret;
}

//main.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h> 

#include "SeqList.h"

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

#define SIZE 20

void print_array(int a[],int len)
{
	int i = 0;
	for(i=0;i<len;i++)
	{
		printf("%d ",a[i]);	
	}
	printf("\n");
}
int static_search(int a[],int len,int key)
{
	int ret = -1;
	int i = 0;
	for(i=0;i<len;i++)
	{
		if(key == a[i])
		{
			ret = i;
			break;	
		}	
	}
	return ret;
}

void static_test()
{
	int array[SIZE] = {0};
	int i = 0;
	int key = 0;
	int index = -1;
	srand((unsigned int)time(NULL));
	for(i=0;i<SIZE;i++)
	{
		array[i] = rand()%100;	
	}
	
	key = rand()%100;
	printf("Static Search Demo!\n");
	
	printf("key : %d\n",key);
	
	printf("ALL :\n");
	
	print_array(array,SIZE);
	
	printf("\n");
		
	index = static_search(array,SIZE,key);
	
	if(index >= 0)
	{
		printf("Success,a[%d] = %d\n",index,key);	
	}
	else
	{
		printf("failed!\n");
	}
}
//------------------------------------------
void print_list(SeqList* list)
{
	int i = 0;
	for(i=0;i<SeqList_Length(list);i++)
	{
		printf("%d ",(int)SeqList_Get(list,i));	
	}
	printf("\n");
}
int dynamic_search(SeqList* list,int key)
{
	int ret = -1;
	int i = 0;
	for(i=0;i<SeqList_Length(list);i++)
	{
		if((int)SeqList_Get(list,i)==key)
		{
			ret = i;
			SeqList_Delete(list,i);
			break;	
		}	
	}
	return ret;
}
void dynamic_test()
{
	SeqList* list = SeqList_Create(SIZE);
	int i = 0;
	int key = 0;
	int index = 0;
	srand((unsigned int)time(NULL));
	
	for(i=0;i<SIZE;i++)
	{
		SeqList_Insert(list,(SeqListNode*)(rand()%100),i);	
	}
	
	printf("Dynamic Search Demo!\n");
	
	key = rand()%100;
	printf("key : %d\n",key);
	
	printf("ALL:\n");
	print_list(list);
	
	index = dynamic_search(list,key);
	
	if(index >= 0)
	{
		printf("Success,List[%d] = %d\n",index,key);	
	}
	else
	{
		printf("failed!\n");	
	}
	print_list(list);
	SeqList_Destroy(list);
}

//------------------------------------------
int main(int argc, char *argv[]) 
{
	static_test();
	dynamic_test();
	return 0;
}


1静态查找的改进:

//FindPro.c 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

#define SIZE 20
void print_array(int a[],int begin,int end)
{
	int i = 0;
	for(i=begin;i<=end;i++)
	{
		printf("%d,",a[i]);	
	}
	printf("\n");
}
int array_search(int a[],int len,int key)
{
	int ret = len;
	
	a[0] = key;

	while(a[ret]!=key)  //与for()...if()...相比,少了一次比较的过程 
	{
		ret--;	
	}
	
	return ret;
}
int main(int argc, char *argv[]) 
{
	int array[SIZE+1] = {0};
	int i = 0;
	int key = 0;
	int index = 0;
	
	srand((unsigned int)time(NULL));
	
	for(i=1;i<=SIZE;i++)
	{
		array[i] = rand()%100;	
	}
	
	key = rand()%100;
	printf("key = %d\n",key);
	
	printf("ALL:\n");
	print_array(array,1,SIZE);
	
	index = array_search(array,SIZE+1,key);	
	
	if(index > 0)
	{
		printf("Success! array[%d] = %d\n",index,key);	
	}
	else
	{
		printf("Failure!\n");	
	}
	
	return 0;
}


1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值