数据结构C语言版本严蔚敏答案.docx

上传人:b****6 文档编号:16800930 上传时间:2023-07-17 格式:DOCX 页数:204 大小:78.57KB
下载 相关 举报
数据结构C语言版本严蔚敏答案.docx_第1页
第1页 / 共204页
数据结构C语言版本严蔚敏答案.docx_第2页
第2页 / 共204页
数据结构C语言版本严蔚敏答案.docx_第3页
第3页 / 共204页
数据结构C语言版本严蔚敏答案.docx_第4页
第4页 / 共204页
数据结构C语言版本严蔚敏答案.docx_第5页
第5页 / 共204页
数据结构C语言版本严蔚敏答案.docx_第6页
第6页 / 共204页
数据结构C语言版本严蔚敏答案.docx_第7页
第7页 / 共204页
数据结构C语言版本严蔚敏答案.docx_第8页
第8页 / 共204页
数据结构C语言版本严蔚敏答案.docx_第9页
第9页 / 共204页
数据结构C语言版本严蔚敏答案.docx_第10页
第10页 / 共204页
数据结构C语言版本严蔚敏答案.docx_第11页
第11页 / 共204页
数据结构C语言版本严蔚敏答案.docx_第12页
第12页 / 共204页
数据结构C语言版本严蔚敏答案.docx_第13页
第13页 / 共204页
数据结构C语言版本严蔚敏答案.docx_第14页
第14页 / 共204页
数据结构C语言版本严蔚敏答案.docx_第15页
第15页 / 共204页
数据结构C语言版本严蔚敏答案.docx_第16页
第16页 / 共204页
数据结构C语言版本严蔚敏答案.docx_第17页
第17页 / 共204页
数据结构C语言版本严蔚敏答案.docx_第18页
第18页 / 共204页
数据结构C语言版本严蔚敏答案.docx_第19页
第19页 / 共204页
数据结构C语言版本严蔚敏答案.docx_第20页
第20页 / 共204页
亲,该文档总共204页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

数据结构C语言版本严蔚敏答案.docx

《数据结构C语言版本严蔚敏答案.docx》由会员分享,可在线阅读,更多相关《数据结构C语言版本严蔚敏答案.docx(204页珍藏版)》请在冰点文库上搜索。

数据结构C语言版本严蔚敏答案.docx

数据结构C语言版本严蔚敏答案

严蔚敏《数据结构(C语言版)习题集》答案

说明:

1.本文是对严蔚敏《数据结构(c语言版)习题集》一书中所有算法设计题目的解决方案,主要作者为一具.以下网友:

biwier,szm99,siice,龙抬头,iamkent,zames,birdthinking,lovebuaa等为答案的修订和完善工作提出了宝贵意见,在此表示感谢;

2.本解答中的所有算法均采用类c语言描述,设计原则为面向交流、面向阅读,作者不保证程序能够上机正常运行(这种保证实际上也没有任何意义);

3.本解答原则上只给出源代码以及必要的注释,对于一些难度较高或思路特殊的题目将给出简要的分析说明,对于作者无法解决的题目将给出必要的讨论.目前尚未解决的题目有:

5.20,10.40;

4.请读者在自己已经解决了某个题目或进行了充分的思考之后,再参考本解答,以保证复习效果;

5.由于作者水平所限,本解答中一定存在不少这样或者那样的错误和不足,希望读者们在阅读中多动脑、勤思考,争取发现和纠正这些错误,写出更好的算法来.请将你发现的错误或其它值得改进之处向作者报告:

  [email]yi-ju@[/email]

第一章绪论

1.16

voidprint_descending(intx,inty,intz)//按从大到小顺序输出三个数

{

  scanf("%d,%d,%d",&x,&y,&z);

  if(xy;//<->为表示交换的双目运算符,以下同

  if(yz;

  if(xy;//冒泡排序

  printf("%d%d%d",x,y,z);

}//print_descending

1.17

Statusfib(intk,intm,int&f)//求k阶斐波那契序列的第m项的值f

{

  inttempd;

  if(k<2||m<0)returnERROR;

  if(m

  elseif(m==k-1||m==k)f=1;

  else

  {

  for(i=0;i<=k-2;i++)temp[i]=0;

  temp[k-1]=1;temp[k]=1;//初始化

  sum=1;

  j=0;

  for(i=k+1;i<=m;i++,j++)//求出序列第k至第m个元素的值

    temp[i]=2*sum-temp[j];

  f=temp[m];

  }

  returnOK;

}//fib

分析:

k阶斐波那契序列的第m项的值f[m]=f[m-1]+f[m-2]+......+f[m-k]

      =f[m-1]+f[m-2]+......+f[m-k]+f[m-k-1]-f[m-k-1]

      =2*f[m-1]-f[m-k-1]

所以上述算法的时间复杂度仅为O(m).如果采用递归设计,将达到O(k^m).即使采用暂存中间结果的方法,也将达到O(m^2).  

1.18

typedefstruct{

              char*sport;

              enum{male,female}gender;

              charschoolname;//校名为'A','B','C','D'或'E'

              char*result;

              intscore;

            }resulttype;

typedefstruct{

              intmalescore;

              intfemalescore;

              inttotalscore;

            }scoretype;

voidsummary(resulttyperesult[])//求各校的男女总分和团体总分,假设结果已经储存在result[]数组中

{

  scoretypescore[MAXSIZE];

  i=0;

  while(result[i].sport!

=NULL)

  {

  switch(result[i].schoolname)

  {

    case'A':

      score[0].totalscore+=result[i].score;

      if(result[i].gender==0)score[0].malescore+=result[i].score;

      elsescore[0].femalescore+=result[i].score;

      break;

    case'B':

      score[0].totalscore+=result[i].score;

      if(result[i].gender==0)score[0].malescore+=result[i].score;

      elsescore[0].femalescore+=result[i].score;

      break;

    ……  ……  ……

  }

  i++;

  }

  for(i=0;i<5;i++)

  {

  printf("School%d:

\n",i);

  printf("Totalscoreofmale:

%d\n",score[i].malescore);

  printf("Totalscoreoffemale:

%d\n",score[i].femalescore);

  printf("Totalscoreofall:

%d\n\n",score[i].totalscore);

  }

}//summary

1.19

Statusalgo119(inta[ARRSIZE])//求i!

*2^i序列的值且不超过maxint

{

  last=1;

  for(i=1;i<=ARRSIZE;i++)

  {

  a[i-1]=last*2*i;

  if((a[i-1]/last)!

=(2*i))reurnOVERFLOW;

  last=a[i-1];

  returnOK;

  }

}//algo119

分析:

当某一项的结果超过了maxint时,它除以前面一项的商会发生异常.

1.20

voidpolyvalue()

{

  floattemp;

  float*p=a;

  printf("Inputnumberofterms:

");

  scanf("%d",&n);

  printf("Inputvalueofx:

");

  scanf("%f",&x);

  printf("Inputthe%dcoefficientsfroma0toa%d:

\n",n+1,n);

  p=a;xp=1;sum=0;//xp用于存放x的i次方

  for(i=0;i<=n;i++)

  {

  scanf("%f",&temp);

  sum+=xp*(temp);

  xp*=x;

  }

  printf("Valueis:

%f",sum);

}//polyvalue

 第二章线性表

2.10

StatusDeleteK(SqList&a,inti,intk)//删除线性表a中第i个元素起的k个元素

{

  if(i<1||k<0||i+k-1>a.length)returnINFEASIBLE;

  for(count=1;i+count-1<=a.length-k;count++)//注意循环结束的条件

  a.elem[i+count-1]=a.elem[i+count+k-1];

  a.length-=k;

  returnOK;

}//DeleteK

2.11

StatusInsert_SqList(SqList&va,intx)//把x插入递增有序表va中

{

  if(va.length+1>va.listsize)returnERROR;

  va.length++;

  for(i=va.length-1;va.elem[i]>x&&i>=0;i--)

  va.elem[i+1]=va.elem[i];

  va.elem[i+1]=x;

  returnOK;

}//Insert_SqList

2.12

intListComp(SqListA,SqListB)//比较字符表A和B,并用返回值表示结果,值为1,表示A>B;值为-1,表示A

{

  for(i=1;i<=A.length&&i<=B.length;i++)

  if(A.elem[i]!

=B.elem[i])

    returnA.elem[i]>B.elem[i]?

1:

-1;

  if(A.length==B.length)return0;

  returnA.length>B.length?

1:

-1;  //当两个字符表可以互相比较的部分完全相同时,哪个较长,哪个就较大

}//ListComp

2.13

LNode*Locate(LinkListL,intx)//链表上的元素查找,返回指针

{

  for(p=l->next;p&&p->data!

=x;p=p->next);

  returnp;

}//Locate

2.14

intLength(LinkListL)//求链表的长度

{

  for(k=0,p=L;p->next;p=p->next,k++);

  returnk;

}//Length

2.15

voidListConcat(LinkListha,LinkListhb,LinkList&hc)//把链表hb接在ha后面形成链表hc

{

  hc=ha;p=ha;

  while(p->next)p=p->next;

  p->next=hb;

}//ListConcat

2.16

见书后答案.

2.17

StatusInsert(LinkList&L,inti,intb)//在无头结点链表L的第i个元素之前插入元素b

{

  p=L;q=(LinkList*)malloc(sizeof(LNode));

  q.data=b;

  if(i==1)

  {

  q.next=p;L=q;//插入在链表头部

  }

  else

  {

  while(--i>1)p=p->next;

  q->next=p->next;p->next=q;//插入在第i个元素的位置

  }

}//Insert

2.18

StatusDelete(LinkList&L,inti)//在无头结点链表L中删除第i个元素

{

  if(i==1)L=L->next;//删除第一个元素

  else

  {

  p=L;

  while(--i>1)p=p->next;

  p->next=p->next->next;//删除第i个元素

  }

}//Delete

2.19

StatusDelete_Between(Linklist&L,intmink,intmaxk)//删除元素递增排列的链表L中值大于mink且小于maxk的所有元素

{

  p=L;

  while(p->next->data<=mink)p=p->next;//p是最后一个不大于mink的元素

  if(p->next)  //如果还有比mink更大的元素

  {

  q=p->next;

  while(q->datanext;//q是第一个不小于maxk的元素

  p->next=q;

  }

}//Delete_Between

2.20

StatusDelete_Equal(Linklist&L)//删除元素递增排列的链表L中所有值相同的元素

{

  p=L->next;q=p->next;//p,q指向相邻两元素

  while(p->next)

  {

  if(p->data!

=q->data)

  {

    p=p->next;q=p->next;//当相邻两元素不相等时,p,q都向后推一步

  }

  else

  {

    while(q->data==p->data)

  {

    free(q);

    q=q->next;

  }

    p->next=q;p=q;q=p->next;//当相邻元素相等时删除多余元素

  }//else

  }//while

}//Delete_Equal

2.21

voidreverse(SqList&A)//顺序表的就地逆置

{

  for(i=1,j=A.length;i

  A.elem[i]<->A.elem[j];

}//reverse

2.22

voidLinkList_reverse(Linklist&L)//链表的就地逆置;为简化算法,假设表长大于2

{

  p=L->next;q=p->next;s=q->next;p->next=NULL;

  while(s->next)

  {

  q->next=p;p=q;

  q=s;s=s->next;//把L的元素逐个插入新表表头

  }

  q->next=p;s->next=q;L->next=s;

}//LinkList_reverse

分析:

本算法的思想是,逐个地把L的当前元素q插入新的链表头部,p为新表表头.

2.23

voidmerge1(LinkList&A,LinkList&B,LinkList&C)//把链表A和B合并为C,A和B的元素间隔排列,且使用原存储空间

{

  p=A->next;q=B->next;C=A;

  while(p&&q)

  {

  s=p->next;p->next=q;//将B的元素插入

  if(s)

  {

    t=q->next;q->next=s;//如A非空,将A的元素插入

  }

  p=s;q=t;

  }//while

}//merge1

2.24

voidreverse_merge(LinkList&A,LinkList&B,LinkList&C)//把元素递增排列的链表A和B合并为C,且C中元素递减排列,使用原空间

{

  pa=A->next;pb=B->next;pre=NULL;//pa和pb分别指向A,B的当前元素

  while(pa||pb)

  {

  if(pa->datadata||!

pb)

  {

    pc=pa;q=pa->next;pa->next=pre;pa=q;//将A的元素插入新表

  }

  else

  {

    pc=pb;q=pb->next;pb->next=pre;pb=q;//将B的元素插入新表

  }

  pre=pc;

  }

  C=A;A->next=pc;//构造新表头

}//reverse_merge

分析:

本算法的思想是,按从小到大的顺序依次把A和B的元素插入新表的头部pc处,最后处理A或B的剩余元素.

2.25

voidSqList_Intersect(SqListA,SqListB,SqList&C)//求元素递增排列的线性表A和B的元素的交集并存入C中

{

  i=1;j=1;k=0;

  while(A.elem[i]&&B.elem[j])

  {

  if(A.elem[i]

  if(A.elem[i]>B.elem[j])j++;

  if(A.elem[i]==B.elem[j])

  {

    C.elem[++k]=A.elem[i];//当发现了一个在A,B中都存在的元素,

    i++;j++;//就添加到C中

  }

  }//while

}//SqList_Intersect

2.26

voidLinkList_Intersect(LinkListA,LinkListB,LinkList&C)//在链表结构上重做上题

{

  p=A->next;q=B->next;

  pc=(LNode*)malloc(sizeof(LNode));

C=pc;

  while(p&&q)

  {

  if(p->datadata)p=p->next;

  elseif(p->data>q->data)q=q->next;

  else

  {

    s=(LNode*)malloc(sizeof(LNode));

    s->data=p->data;

    pc->next=s;pc=s;

    p=p->next;q=q->next;

  }

  }//while

}//LinkList_Intersect

2.27

voidSqList_Intersect_True(SqList&A,SqListB)//求元素递增排列的线性表A和B的元素的交集并存回A中

{

  i=1;j=1;k=0;

  while(A.elem[i]&&B.elem[j])

  {

  if(A.elem[i]

  elseif(A.elem[i]>B.elem[j])j++;

  elseif(A.elem[i]!

=A.elem[k])

  {

    A.elem[++k]=A.elem[i];//当发现了一个在A,B中都存在的元素

    i++;j++;//且C中没有,就添加到C中

  }

  else{i++;j++;}

  }//while

  while(A.elem[k])A.elem[k++]=0;

}//SqList_Intersect_True

2.28

voidLinkList_Intersect_True(LinkList&A,LinkListB)//在链表结构上重做上题

{

  p=A->next;q=B->next;pc=A;

  while(p&&q)

  {

  if(p->datadata)p=p->next;

  elseif(p->data>q->data)q=q->next;

  elseif(p->data!

=pc->data)

  {

    pc=pc->next;

    pc->data=p->data;

    p=p->next;q=q->next;

  }

  }//while

}//LinkList_Intersect_True

2.29

voidSqList_Intersect_Delete(SqList&A,SqListB,SqListC)

{

  i=0;j=0;k=0;m=0;    //i指示A中元素原来的位置,m为移动后的位置

  while(i

  {

  if(B.elem[j]

  elseif(B.elem[j]>C.elem[k])k++;

  else

  {

    same=B.elem[j];            //找到了相同元素same

    while(B.elem[j]==same)j++;

    while(C.elem[k]==same)k++;    //j,k后移到新的元素

    while(i

      A.elem[m++]=A.elem[i++];        //需保留的元素移动到新位置

    while(i

  }

  }//while

  while(i

  A.elem[m++]=A.elem[i++];    //A的剩余元素重新存储。

  A.length=m;

}//SqList_Intersect_Delete

分析:

先从B和C中找出共有元素,记为same,再在A中从当前位置开始,凡小于same的

元素均保留(存到新的位置),等于same的就跳过,到大于same时就再找下一个same.

2.30

voidLinkList_Intersect_Delete(LinkList&A,LinkListB,LinkListC)//在链表结构上重做上题

{

  p=B->next;q=C->next;r=A-next;

  while(p&&q&&r)

  {

  if(p->datadata)p=p->next;

  elseif(p->data>q->data)q=q->next;

  else

  {

    u=p->data;//确定待删除元素u

    while(r->next->datanext;//确定最后一个小于u的元素指针r

    if(r->next->data==u)

    {

      s=r->next;

      while(s->data==u)

      {

      t=s;s=s->next;free(t);//确定第一个大于u的元素指针s

      }//while

      r->next=s;//删除r和s之间的元素

    }//if

    while(p->data=u)p=p->next;

    while(q->data=u)q=q->next;

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2