数据结构习题集答案解析清华大学版

}

}

} i++;

if(!StackEmpty(s)){ }

else return FALSE;

if(!StackEmpty(s)) return FALSE; return OK;

Pop(s,c1); strcpy(a,c1);

void InitStack(Stack &s) { }

Status Push(Stack &s,ElemType e) { }

Status Pop(Stack &s,ElemType e) { }

Status StackEmpty(Stack s) { }

if(s、size==0) return TRUE; else return FALSE; LinkType p; if(s、top){ }

return OK;

strcpy(e,s、top->data); p=s、top; s、top=p->next; delete p; s、size--; LinkType p; p=new NodeType; if(!p) exit(OVERFLOW); p->next=s、top; s、top=p;

strcpy(p->data,e); s、size++; return OK; s、top=NULL; s、size=0;

Status IsOperator(char c) { }

3、24 试编写如下定义得递归函数得递归算法,并根据算法画出求g(5,2)时栈得变化过程。

解:

int g(int m,int n); int main() { }

int g(int m,int n) { }

假设主函数得返回地址为0,递归函数3条语句得地址分别为1、2、3。

3 3 3 3 3 0

0 1 2 3 4 5

64 32 16 8 4 2

if(m>0)

return(g(m-1,2*n)+n); else return 0; int m,n;

cout<<\请输入m与n得值:\cin>>m>>n;

if(n>=0) cout<

char *p=\while(*p){ }

return FALSE;

if(*p==c)

return TRUE; p++;

0m?0,n?0? g?m,n?????gm?1,2n?nm?0,n?0?3、25 试写出求递归函数F(n)得递归算法,并消除递归:

解:

#include #define N 20 int main()

??n?1F?n???n?Fn?2???n?0n?0

{ }

3、26 求解平方根

int i; int a[N]; int n;

cout<<\请输入n:\cin>>n;

for(i=0;i

cout<

if(i<1) a[i]=1; else a[i]=i*a[i/2];

A得迭代函数定义如下:

?p?sqrt?A,p,e????1?A????sqrt?A,2??p?p??,e???????p2?A?ep?A?e2

其中,p就是A得近似平方根,e就是结果允许误差。试写出相应得递归算法,并消除递归。

解:

#include

double Sqrt(double A,double p,double e); int main() { }

double Sqrt(double A,double p,double e) { }

3、27 已知Ackerman函数得定义如下:

if((p*p-A)>-e && (p*p-A)

return p;

return Sqrt(A,(p+A/p)/2,e); else

double A,p,e;

cout<<\请输入A p e:\cin>>A>>p>>e;

cout<

n?1m?0??akm?m,n???akm?m?1,1?m?0,n?0

?akm?m?1,akm?m,n?1??m?0,n?0? (1) 写出递归算法;

(2) 写出非递归算法;

(3) 根据非递归算法,画出求akm(2,1)时栈得变化过程。

解:

unsigned int akm(unsigned int m,unsigned int n) { }

非递归算法:

int akm1(int m,int n) { }

0,akm(2,1) 0 2 1 1,akm(2,0) 6 2 0 2,akm(1,1) 4 1 1 3,akm(1,0) 6 1 0

g=akm(2,0)

akm=akm(m-1,1)=akm(1,1) g=akm(m,n-1)=akm(1,0) akm=akm(m-1,1)=akm(0,1)

Stack s; InitStack(s); ElemType e,e1,d; e、mval=m; Push(s,e); do{

while(e、mval){ }

if(StackLength(s)>1){ }

e1、nval=e、nval; Pop(s,e); e、mval--;

e、nval=e1、nval+1; while(e、nval){ }

e、mval--;

e、nval=1;

e、nval--; Push(s,e);

e、nval=n;

unsigned int g; if(m==0)

return n+1;

if(n==0) return akm(m-1,1); else{ }

g=akm(m,n-1); return akm(m-1,g);

else

}while(StackLength(s)!=1||e、mval!=0); return e、nval+1;

4,akm(0,1) 4 0 1 0,akm(2,1) 0 2 1 1,akm(2,0) 6 2 0 2,akm(1,1) 4 1 1 3,akm(1,0) 6 1 0 0,akm(2,1) 0 2 1 1,akm(2,0) 6 2 0 2,akm(1,1) 4 1 1 3,akm(0,2) 7 0 2 akm=n+1=2 退栈 g=akm(2,0)

akm=akm(m-1,1)=akm(1,1) g=akm(m,n-1)=akm(1,0)

akm=akm(m-1,1)=akm(0,1)=2 退栈 g=akm(2,0)

akm=akm(m-1,1)=akm(1,1)

g=akm(m,n-1)=akm(1,0)=2; akm=akm(m-1,g)=akm(0,2);鏍嘔鷓幗礦雏軛。 akm=n+1=3 退栈 0,akm(2,1) 0 2 1 1,akm(2,0) 6 2 0 2,akm(1,1) 4 1 1

顫莖镝。 0,akm(2,1) 0 2 1 1,akm(2,0) 6 2 0 0,akm(2,1) 0 2 1 1,akm(1,3) 6 1 3 2,akm(1,2) 6 1 2 3,akm(1,1) 6 1 1 4,akm(1,0) 6 1 0 5,akm(0,1) 4 0 1 0,akm(2,1) 0 2 1 1,akm(1,3) 6 1 3 2,akm(1,2) 6 1 2 3,akm(1,1) 6 1 1 4,akm(1,0) 6 1 0 0,akm(2,1) 0 2 1 1,akm(1,3) 6 1 3 2,akm(1,2) 6 1 2 3,akm(1,1) 6 1 1 4,akm(0,2) 7 0 2 0,akm(2,1) 0 2 1 1,akm(1,3) 6 1 3 2,akm(1,2) 6 1 2 3,akm(1,1) 6 1 1 0,akm(2,1) 0 2 1 1,akm(1,3) 6 1 3 2,akm(1,2) 6 1 2 3,akm(0,3) 7 0 3 0,akm(2,1) 0 2 1 1,akm(1,3) 6 1 3 2,akm(1,2) 6 1 2 0,akm(2,1) 0 2 1 1,akm(1,3) 6 1 3

g=akm(2,0)

akm=akm(m-1,1)=akm(1,1)

g=akm(m,n-1)=akm(1,0)=2; akm=akm(m-1,g)=akm(0,2)=3; 退栈肃郧况簡 g=akm(2,0)

akm=akm(m-1,1)=akm(1,1)=3 退栈 g=akm(2,0)=3; akm=akm(1,3) g=akm(1,2); akm(m-1,g) g=akm(1,1); akm(m-1,g) g=akm(1,0); akm(m-1,g) akm=akm(0,1) akm(0,1)=2退栈

g=akm(2,0)=3; akm=akm(1,3) g=akm(1,2); akm(m-1,g) g=akm(1,1); akm(m-1,g) g=akm(1,0); akm(m-1,g) akm=akm(0,1)=2退栈 g=akm(2,0)=3; akm=akm(1,3) g=akm(1,2); akm(m-1,g) g=akm(1,1); akm(m-1,g)

g=akm(1,0)=2; akm(m-1,g)=akm(0,2)鴉厦纤璦緋羅呕。 akm=n+1=3 退栈

g=akm(2,0)=3; akm=akm(1,3) g=akm(1,2); akm(m-1,g) g=akm(1,1); akm(m-1,g)

g=akm(1,0)=2; akm(m-1,g)=akm(0,2)=3退栈冑堅壮攛览個毂。 g=akm(2,0)=3; akm=akm(1,3) g=akm(1,2); akm(m-1,g)

g=akm(1,1)=3; akm(m-1,g)=akm(0,3)讕陨鎮鹫锞蒞轎。 akm=n+1=4 退栈

g=akm(2,0)=3; akm=akm(1,3) g=akm(1,2); akm(m-1,g)

g=akm(1,1)=3; akm(m-1,g)=akm(0,3)=4 退栈織赇饺漣难娄锷。 g=akm(2,0)=3; akm=akm(1,3)

g=akm(1,2)=4; akm(m-1,g)=akm(0,4)龅颠鈹语骧紼秽。

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4