查找的实现

#define maxsize 100
typedef struct
{
 datatype data[maxsize];
 int length;
} Slist;

静态查找表
1、顺序查找
int Search_sequ(Slist st,datatype x)
{
 int i;
 for(i=0;i<st.length;i++)
  if(st.data[i]==x)
   return i;
 if(i>=st.length)
   return 0; 
}

2、二分查找
有序表的查找
int Search_bina(stable st,datatype x)
{
 int Low,High;
 Low=0;
 High=st.length-1;
 while(low<=high)
 {
  mid=(low+high)/2;
  if(k==st.data[mid])
   return mid;
  else if(k>st.data[mid])
   Low=mid+1;
  else
   High=mid-1;
  
 }
 return 0;
}

3、分快查找
分块查找实际上进行了两次查找。

动态查找
1、二叉排序树

typedef struct
{
 Keytype key;
 Other info;
} Elemtype;
typedef struct Bnode
{
 Elemtype data;
 Elemtype *lchild,*rchild;
} Bitree;

int Search_bst(Bitree *t,Keytype k,Bitree &p,Bitree &f)
{
 f=NULL;
 p=t;
 while(p)
 {
  if(k==p->data.key)
   return 1;
  else if(k<p->data.key)
  {
   f=p;
   p=p->lchild;
  }
  else
  {
   f=p;
   p=p->rchild;
  }
 }
 return 0;
}

int Insert_bst(Bitree t,Elemtype x)
{
 Bnode *s;
 f=NULL;
 if(Search_bst(t,x.Keytype,p,f))
  return 0;
 else 
 {
  s=malloc(sizeof(Bnode));
  s->data=x;
  s->lchild=NULL:
  s->rchild=NULL;
  if(!f)
   t=s;
  else if(x.key<f->data.key)
   f->lchild=s;
  else
   f->rchild=s;
  return 1; 
  } 
   } 
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值