山东财经大学 - 数据结构试题 下载本文

head=p->next; p->next=0;

if (p->data>='A' && p->data<='Z') {p->next=ha; ha=p;}

else if (p->data>='0' && p->data<='9') {p->next=hb; hb=p;} else {p->next=hc; hc=p;} } }

2. 设计在链式存储结构上交换二叉树中所有结点左右子树的算法。

typedef struct node {int data; struct node *lchild,*rchild;} bitree; void swapbitree(bitree *bt) {

bitree *p; if(bt==0) return;

swapbitree(bt->lchild); swapbitree(bt->rchild); p=bt->lchild; bt->lchild=bt->rchild; bt->rchild=p; }

3. 在链式存储结构上建立一棵二叉排序树。

#define n 10

typedef struct node{int key; struct node *lchild,*rchild;}bitree; void bstinsert(bitree *&bt,int key) {

if (bt==0){bt=(bitree *)malloc(sizeof(bitree)); bt->key=key;bt->lchild=bt->rchild=0;} else if (bt->key>key) bstinsert(bt->lchild,key); else bstinsert(bt->rchild,key); }

void createbsttree(bitree *&bt) { int i;

for(i=1;i<=n;i++) bstinsert(bt,random(100)); }

45

数据结构试卷(五)参考答案

一、选择题 1.A 6.B

二、填空题 1. top1+1=top2

2. 可以随机访问到任一个顶点的简单链表 3. i(i+1)/2+j-1 4. FILO,FIFO

5. ABDECF,DBEAFC,DEBFCA 6. 8,64 7. 出度,入度 8. ki<=k2i && ki<=k2i+1 9. n-i,r[j+1]=r[j]

10. mid=(low+high)/2,r[mid].key>k

三、应用题 2. DEBCA

3. E={(1,5),(5,2),(5,3),(3,4)},W=10 4. ASL=(1*1+2*2+3*4)/7=17/7 5. ASL1=7/6,ASL2=4/3 四、算法设计题

1. 设计判断两个二叉树是否相同的算法。

typedef struct node {datatype data; struct node *lchild,*rchild;} bitree; int judgebitree(bitree *bt1,bitree *bt2) {

if (bt1==0 && bt2==0) return(1);

else if (bt1==0 || bt2==0 ||bt1->data!=bt2->data) return(0);

46

2.B 7.B

3.A 8.B

4.A 9.C

5.D 10.C

else return(judgebitree(bt1->lchild,bt2->lchild)*judgebitree(bt1->rchild,bt2->rchild)); }

2. 设计两个有序单链表的合并排序算法。

void mergelklist(lklist *ha,lklist *hb,lklist *&hc) {

lklist *s=hc=0; while(ha!=0 && hb!=0)

if(ha->datadata){if(s==0) hc=s=ha; else {s->next=ha; s=ha;};ha=ha->next;} else {if(s==0) hc=s=hb; else {s->next=hb; s=hb;};hb=hb->next;} if(ha==0) s->next=hb; else s->next=ha; }

数据结构试卷(六)参考答案

一、选择题 1.D 6.D

2.A 7.B

3.A 8.A

4.A 9.C

5.D 10.B

11.C 12.A 13.B 14.D 15.B

二、判断题

1.错 2.对 3.对 4.对 5.错 6.错 7.对 8.错 9.对 10.对

三、填空题 1. O(n)

2. s->next=p->next; p->next=s

47

3. (1,3,2,4,5) 4. n-1 5. 129 6. F==R

7. p->lchild==0&&p->rchild==0 8. O(n2)

9. O(nlog2n), O(n) 10. 开放定址法,链地址法

四、算法设计题

1. 设计在顺序有序表中实现二分查找的算法。

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

2. 设计判断二叉树是否为二叉排序树的算法。 int minnum=-32768,flag=1;

typedef struct node{int key; struct node *lchild,*rchild;}bitree; void inorder(bitree *bt) { if

(bt!=0)

{inorder(bt->lchild);

if(minnum>bt->key)flag=0;

minnum=bt->key;inorder(bt->rchild);} }

48