d>q->id)
q=q->next;
else
{ if (p->score<60)
if (q->score<60)
p->score=q->score;
else p->score=60;
p=p->next;
q=q->next;
}
}
}
32.阅读下列算法,并回答问题:
(1)设串s=“OneWorldOneDream”,t="One",pos是一维整型数组,写出算法
f32(s,t,pos)执行之后得到的返回值和pos中的值;
(2)简述算法f32的功能。
int strlen(char*s); /*返回串s的长度*/
int index(char*st,char*t);
/*若串t在串st中出现,则返回在串st中首次出现的下标值,否则返回-1*/
int f32(char*s, char*t, int pos[])
{ int i, j, k, ls, lt;
ls=strlen(s);
1t=strlen(t);
if (ls= =0||1t= =0) return-1;
k=0;
i=0;
do {
j=index(s+i, t);
if (j>=0)
{ pos[k++]=i+j;
i+=j+1t;
}
}while(i+1t<=1s && j >=0);
return k;
}
33.二叉排序树的存储结构定义为以下类型:
typedef int KeyType;
typedef struct node {
KeyType key; /*关键字项*/
InfoType otherinfo; /*其它数据项*/
struct node *1child, *rchild; /*左、右孩子指针*/
} BSTNode, *BSTree;
阅读算法f33,并回答问题:
(1)对如图所示的二叉排序树T,写出f33(T,8)
返回的指针所指结点的关键字;
(2)在哪些情况下算法f33返回空指针?
(3)简述算法f33的功能。
BSTNode *f33(BSTree T, KeyType x)
{ BSTNode *p;
if (T= =NULL) return NULL;
p=f33(T->1child, x);
if (p!=NULL)return p;
if (T->key>x)return T;
return f33(T-> rchild, x);
}
五、算法设计题(本题10分)
34.假设线性表采用顺序存储结构,其类型定义如下:
#define ListSize 100
typedef struct {
int data[ListSize];
int length;
} SeqList, *Table;
编写算法,将顺序表L中所有值为奇数的元素调整到表的前端。