数据结构与算法分析 第6章 答案 Larry Nyhoff 清华大学.pdf
《数据结构与算法分析 第6章 答案 Larry Nyhoff 清华大学.pdf》由会员分享,可在线阅读,更多相关《数据结构与算法分析 第6章 答案 Larry Nyhoff 清华大学.pdf(20页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Chapter 6 26 Chapter 6:ListsExercises 6.31-6./-Polynomial.h-#include#ifndef POLYNOMIAL#define POLYNOMIALconst int MAX_DEGREE=100;typedef double CoefType;class Polynomial public:void read(istream&in);void display(ostream&out)const;Polynomial operator+(const Polynomial&poly);Polynomial operator*(const
2、 Polynomial&poly);CoefType evaluate(double value);private:int myDegree;CoefType myCoeffMAX_DEGREE+1;istream&operator(istream&in,Polynomial&p);ostream&operator(ostream&out,const Polynomial&p);#endif/-Polynomial.cpp-#include#include using namespace std;#include Polynomial.h/-Definition of istream&oper
3、ator(istream&in,Polynomial&p)p.read(in);return in;/-Definition of read()void Polynomial:read(istream&in)cout Enter the degree(=MAX_DEGREE myDegree;/the degree of this polynomial assert(myDegree=MAX_DEGREE);Chapter 6 27 CoefType co;/the coefficients in ascending order cout Enter coefficients in ascen
4、ding order:n;for(int index=0;index co;myCoeffindex=co;/-Definition of ostream&operator(ostream&out,const Polynomial&p)p.display(out);return out;/-Definition of display()void Polynomial:display(ostream&out)const for(int index=0;index myDegree;index+)out myCoeffindex x index +;out myCoeffmyDegree x my
5、Degree endl;/-Definition of evaluate()CoefType Polynomial:evaluate(CoefType value)CoefType power=1,result=0;for(int index=0;index=myDegree;index+)result+=myCoeffindex*power;power*=value;return result;/-Definition of+Polynomial Polynomial:operator+(const Polynomial&b)Polynomial c;if(myDegree b.myDegr
6、ee)c=b;for(int i=0;i=myDegree;i+)c.myCoeffi+=myCoeffi;c.myDegree=b.myDegree;else c=*this;Chapter 6 28 for(int i=0;i=b.myDegree;i+)c.myCoeffi+=b.myCoeffi;while(c.myCoeffc.myDegree=0)c.myDegree-;return c;/-Definition of*Polynomial Polynomial:operator*(const Polynomial&b)Polynomial c;c.myDegree=myDegre
7、e+b.myDegree;for(int i=0;i=c.myDegree;i+)c.myCoeffi=0;for(int i=0;i=myDegree;i+)for(int j=0;j data and ptr-next refer to the data part andthe next part,respectively,of the node pointed to by ptr.1.Algorithm to count the nodes in a linked listcount=0;ptr=first;while(ptr!=null_value)count+;ptr=ptr-nex
8、t;2.Algorithm to compute average value in a linked list of real numberstotal=0.0;count=0;ptr=first;while(ptr!=null_value)count+;total+=ptr-data;ptr=ptr-next;Chapter 6 29 if(count=0)average=0.0;else average=total/count;3.Algorithm to append a node at the end of a linked listGet a node pointed to by n
9、ewPtr;newPtr-data=item;if(first=null_value)first=newPtr;newPtr-next=null_valueelse ptr=first;while(ptr-next!=null_value)ptr=ptr-next;newPtr-next=ptr;4.Algorithm to determine if the nodes in a linked list are in non-decreasing order.ptr=first;if(ptr=null_value|/note short-circuit eval newPtr-next=nul
10、l_value)return true;else nextPtr=ptr-next;for(;)if(nextPtr=null_value)return true;if(ptr-data nextPtr-data)return false;/else ptr=nextPtr;nextPtr=nextPtr-next;5.All algorithms in exercises#1-#4 require a scan of the entire linked list.Hence,they areO(n).Chapter 6 30 6.Algorithm to search a linked li
11、st for a given item.ptr=first;prevPtr=null_valuefor(;)if(ptr=null_value)return null_value;if(ptr-data=item)return prevPtr;/else prevPtr=ptr;ptr=ptr-next;7.Algorithm to insert a node pointed to by newPtr after the nth node.ptr=first;num=1;Get a node pointed to by newPtr;if(n=0)newPtr-next=ptr;first=n
12、ewPtr;else while(ptr!=null_value&num next;if(ptr!=null_value)newPtr-next=ptr-next;ptr=newPtr;else Signal error:fewer than n elements in list8.Algorithm to delete the nth node in a linked list.ptr=first;num=1;if(n=1)tempPtr=ptr;first=ptr-next;delete tempPtr;Chapter 6 31 else while(ptr!=null_value&num
13、 next;if(ptr!=null_value&/note short-circuit eval ptr-next!=null_value)tempPtr=ptr-next;ptr-next=temPtr-next;Delete node pointed to by tempPtr;else Signal error:fewer than n elements in list9.Algorithm to perform a shuffle-merge of two linked listsGet a node pointed to by mergePtr;/temporary head no
14、delast=mergePtr;ptr1=first1;/runs through first listptr2=first2;/runs through second listwhile(ptr1!=null_value&ptr2!=null_value)Get a node pointed to by tempPtr;tempPtr-data=ptr1-data;last-next=tempPtr;last=tempPtr;ptr1=ptr1-next;Get a node pointed to by tempPtr;tempPtr-data=ptr2-data;last-next=tem
15、pPtr;last=tempPtr;ptr2=ptr2-next;while(ptr1!=null_value)Get a node pointed to by tempPtr;tempPtr-data=ptr1-data;last-next=tempPtr;last=tempPtr;ptr1=ptr1-next;Chapter 6 32 while(ptr2!=null_value)Get a node pointed to by tempPtr;tempPtr-data=ptr2-data;last-next=tempPtr;last=tempPtr;ptr2=ptr2-next;/Del
16、ete temporary headnodetempPtr=mergePtr;mergePtr=mergePtr-next;Delete tempPtr;10.Algorithm to perform a shuffle-merge without copyingif(first1=null_value)mergePtr=first2;else mergePtr=first1;ptr1=first1;/runs through first list ptr2=first2;/runs through second list while(ptr1!=null_value&ptr2!=null_v
17、alue)tempPtr=ptr1;ptr1=tempPtr-next;tempPtr-next=ptr2;if(ptr2!=null_value)tempPtr=ptr2;ptr2=tempPtr-next;tempPtr-next=ptr1;if(ptr1=null_value)tempPtr-next=ptr2;11.Algorithm to merge two non-decreasing listsGet a node pointed to by mergePtr;/temporary head nodelast=mergePtr;ptr1=first1;ptr2=first2;wh
18、ile(ptr1!=null_value&ptr2!=null_value)Get a node pointed to by tempPtr;Chapter 6 33 if(ptr1-data data);tempptr-data=ptr1-data;last-next=tempPtr;last=tempptr;ptr1=ptr1-next;else tempptr-data=ptr2-data;last-next=tempptr;last=tempptr;ptr2=ptr2-next;while(ptr1!=null_value)Get a node pointed to by tempPt
19、r;tempptr-data=ptr1-data;last-next=tempptr;last=tempptr;ptr1=ptr1-next;while(ptr2!=null_value)Get a node pointed to by tempPtr;tempptr-data=ptr2-data;last-next=tempptr;last=tempptr;ptr2=ptr2-next;tempptr=mergePtr;mergePtr=mergePtr-next;/Delete temporary headnodeDelete node pointed to by tempptr;12.A
20、lgorithm to reverse a linked list without copying.currentPtr=first;prevPtr=null_value;while(currentPtr!=null_value)nextPtr=currentPtr-next;currentPtr-next=prevPtr;prevPtr=currentPtr;currentPtr=nextPtr;first=prevPtr;Chapter 6 34 Exercises 6.51.123 4562.34 343.34 344.Error because the next field of p2
21、 is a null pointer5.12 343434346.111 2222221117.34 34348.9.10.11.p4-next is a null pointer,so an error occurs when we attempt to access its data part.p1=p2CatDogEweRatp2=p2p3=p2p4=p2p4=p2CatDogEweRatp1=p2p2=p2p3=p2CatDogEweRatp2=p2p3=p2p4=p2p1=p2Chapter 6 35 12.13.14.15.16.17.CatDogEweRatp2=p2p3=p2p
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构与算法分析 第6章 答案 Larry Nyhoff 清华大学 数据结构 算法 分析
限制150内