#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;
}
}
}