网站大量收购独家精品文档,联系QQ:2885784924

(精品课件)第五次chapter5-6作业(10.25评讲).doc

(精品课件)第五次chapter5-6作业(10.25评讲).doc

  1. 1、本文档共6页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
(精品课件)第五次chapter5-6作业(10.25评讲)

设计递归算法将十进制正整数转成二进制并输出。 void convert(int x){ if(x1){ convert(x/2); coutx%2;} else coutx ;} 写递归算法,在长度为n的数组A中查找item,成功返回位序,失败返回-1 int find(int a[],int n,int item)//从后往前查找, {if (n=0)return -1;//如数组长度n小于或等于0,则失败 else if (a[n-1]==item)//如尾部元素为item,成功 return n-1; else return find(a,n-1,item); //否则,在长度为n-1的数组中继续用同样的方法进行查找 } 顺序表下删除第position个位置之后的k个元素的方法 template class List_entry Error_code ListList_entry :remove_k(int position,int k) { int j; if (position0 || k=0 || positon+kcount) return range_error; for (j=position+k;jcount;j++) entry[j-k]=entry[j]; count-=k; return success; } 2、逆置 (a)//顺序表下的逆置 template class List_entry Error_code ListList_entry :: reverse() { List_entry temp; for (int i=0,j=count-1;i=count/2-1;i++,j--) { temp=entry[i]; entry[i]=entry[j]; entry[j]=temp; } return success; } (b)//简单单链表的逆置,直接利用原表空间 template class List_entry Error_code ListList_entry :: reverse() { NodeList_entry *p,*q; if (count1){ p=head-next; head-next=NULL; while (p){ q=p-next; p-next=head; head=p; p=q; } } return success; } (c)//与线性表存储方案无关的逆置算法 template class List_entry Error_code ListList_entry :: reverse() { for (int i=0;isize();i++){ remove(i,item); insert(0,item); } return success; } template class List_entry Error_code ListList_entry :: reverse() { int i,j; List_entry itema,itemb; for (i=0,j=size()-1;ij;i++,j--) retrieve(i,itema); retrieve(j,itemb); replace(i,itemb); replace(j,itema); } (d)//简单单链表逆置递归算法 template class List_entry Error_code ListList_entry :: reverse() { head=rec_reverse(head); } template class List_entry NodeList_entry* ListList_entry :: rec_reverse(NodeList_entry *head) { if(head==null) return head; NodeList_entry *p=head-next; if(p==null) return head; else{ NodeList_entry *new_head=rec_reverse(p); p-next=head; head-next=null; return new_head; } } E6.1 E6,E7 Given the methods for lists described in this section, write functions to do each of the following tasks. Be sure to specify the preconditions and postcon

文档评论(0)

yan698698 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档