严蔚敏《数据结构》习题(四)

严蔚敏《数据结构》习题(四)

5.28 采用教科书5.6节中给出的m元多项式的表示方法,写一个求m元多项式中第一变元的偏导数的算法。

Status MPList_Partial(MPList &L){
	//对广义表存储结构的多元多项式求第一变元z的偏导
	for(p = L->hp->tp;p && p->exp;t = p,p = p->tp){//t为前驱
		if(p->tag)//表
            Partial_coef(p->hp,p->exp);
		else p->coef *= p->exp;//原子
		p->exp--;
    }
	pre->tp = NULL;
	if(p)	free(p);//删除常数项
    return OK;
}
    
Status  Partial_coef(MPList &L,int e){//递归乘以e
	for(p = L;p;p = p->tp){
		if(!p->tag)	p->coef *= e;
		else Partial_coef(p->hp,e);
    }
    return OK;
}

5.30 试按表头、表尾的分析方法重写求广义表的深度的递归算法。

int GList_Depth(GList L){
    //扩展线性链表存储方式
    if(!L)	return 1;//空表
    if(L->tag == ATOM)	return 0;//原子
    for(max = 0,pp = L->hp; pp; pp = pp->tp){
        dep = GList_Depth(pp); // 求以pp为头指针的子表深度
        if(dep > max)	max = dep;
    }
   return max + 1; // 非空表的深度是各元素的深度的最大值加1     
}

5.32 试编写判别两个广义表是否相等的递归算法。

Status GList_Judge(GList A,GList B){
    //头尾链表,相等返回TRUE,反之FALSE
    if(!A && !B)  return TRUE;
    if(A && B){
        if(A->tag == ATOM && B->tag == ATOM){//原子
        	if(A->atom == B->atom)  return TRUE;
       	 	else    return FALSE;    
    	}
   		else if(A->tag == LIST && B->tag == LIST){//表
        	if(GList_Judge(A->ptr.hp, B->ptr.hp) 
               			&& GList_Judge(A->ptr.tp, B->ptr.tp))//递归
            	return TRUE;
    	} 
    }
   return FALSE; 
}       

5.34 试编写递归算法,逆转广义表中的数据元素。

例如:将广义表 ( a , ( b , c ) , ( ) ) , ( ( ( d ) , e ) , f )

逆转为: ( ( f , ( e , ( d ) ) ) , ( ( ) , ( c , b ) ) , a ).

GList GList_Reverse(GList &L){ 
    //头尾链表存储
    GList A,B,C,D;
    if(!L)	A = NULL;
    else{
        if(L->tag == ATOM){//处理原子
            A = (GList)malloc(sizeof(GLNode));
            A->tag = ATOM;
            A->atom = L->atom;
        }
        else{
            B = GList_Reverse(L->ptr.hp);//处理表头
            if(L->ptr.tp){//产生表尾的逆置广义表
                C = GList_Reverse(L->ptr.tp);
                D = C;
                while(D->ptr.tp)	D = D->ptr.tp;
                D->ptr.tp = (GList)malloc(sizeof(GLNode));
                D = D->ptr.tp;
                D->tag = LIST;
                D->ptr.tp = NULL;
                D->ptr.hp = B;
                D->ptr.tp = C;//连接
            }
            else{
                A = (GList)malloc(sizeof(GLNode));
                A->tag = LIST;
                A->ptr.tp = NULL;
                A->ptr.hp = B;//头结点指向广义表
            }
        }
    }
    return A;
}  
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值