数据结构期末考试复习--2
Posted ygjzs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构期末考试复习--2相关的知识,希望对你有一定的参考价值。
算法设计题--2
six
顺序表中实现二分查找
struct record
{
int key;
int others;
};
int bisearch(struct record r[],int k)
{
int low = 0,mid,high = n-1;
while(low<=high)
{
mid(low+high)/2;
if(r[mid].key==k) return (mid+1);
else if(r[mid].key>k) high = mid-1;
else low = mid+1;
}
return 0;
}
判断二叉树是否为二叉排序树
int minnum = -32768,flag = 1;
typedef struct node
{
int key;
struct node *lchild,*rchild;
}bitree;
void inoeder(bitree *bt)
{
if(bt!=0)
{
inoeder(bt->lchild);
if(minnum>bt->key) flag = 0;
minnum = bt->key;
inoeder(bt->rchild);
}
}
链式结构上直接插入排序
void straightinsertsort(lklist *&head)
{
lklist *s,*p,*q;
int t;
if(head==0||head->next==0) return;
else for(q=head,p=head->next;p!=0;p=q->next)
{
for(s=head;s!=q->next;s=s->next) if(s->data>p->data) break;
if(s==q->next) q=p;
else{
q->next=p->next;
p->next=s->next;
s->next=p;
t=p->data;
p->data=s->data;
s->data=t;
}
}
}
seven
链式结构实现简单选择排序
void simpleselectsorlklist(lklist *&head)
{
lklist *p,*q,*s;
int min,t;
if(head==0||head->next==0) return;
for(q=head;q!=0;q=q->next)
{
min=q->data;
s=q;
for(p=q->next;p!=0;p->p->next)
if(min>p->data){
min = p->data;
s=p;
}
if(s!=q)
{
t=s->data;
s->data=q->data;
q->data=t;
}
}
}
顺序表上实现求子串的算法
void substring(char s[],long start,long count,char t[])
{
long i,j,length = strlen(s);
if(start<1||start>length) cout<<"error
";
else if(start+count-1>length) cout<<"error
"
else{
for (i=start-1,j=0;i<start+cout-1;i++,j++)
{
t[j]=s[i];
t[j]='