数据结构与算法分析 第9章 答案 Larry Nyhoff 清华大学.pdf
《数据结构与算法分析 第9章 答案 Larry Nyhoff 清华大学.pdf》由会员分享,可在线阅读,更多相关《数据结构与算法分析 第9章 答案 Larry Nyhoff 清华大学.pdf(19页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Chapter 9 96 Chapter 9:ADT Implementations:Templates and Standard ContainersExercises 9.31.template DataType average(DataType a,DataType b)return(a+b)/2;2.template DataType max(DataType a,DataType b)if(a b)return a;else return b;3.template DataType median(DataType a,DataType b,DataType c)DataType ma
2、x=a,min=b;if(a max)return max;else if(c min)return min;else return c;4.template DataType arraySum(DataType x,int length)DataType sum=x0;for(int index=1;index length;index+)sum+=xindex;return sum;Chapter 9 97 5.template void arrayMaxMin(DataType x,int length,DataType&min,DataType&max)min=max=x0;for(i
3、nt i=1;i length;i+)if(xi max)max=xi;6.template int search(DataType x,int length,DataType target)for(int index=0;index length;index+)if(xindex=target)return index;return-1;/index of-1 denotes not found7./*-ListT.h-This header file defines the data type List for processing lists.Basic operations are:C
4、onstructor empty:Check if list is empty insert:Insert an item erase:Remove an item display:Output the list :Output operator-*/#include#ifndef LISTT#define LISTTconst int CAPACITY=1024;template class List public:/*Function Members*/*Class constructor*/List();/*-Construct a List object.Precondition:No
5、ne Postcondition:An empty List object has been constructed;mySize is 0.-*/Chapter 9 98 /*empty operation*/bool empty()const;/*-Check if a list is empty.Precondition:None Postcondition:true is returned if the list is empty,false if not.-*/*insert and erase*/void insert(ElementType item,int pos);/*-In
6、sert a value into the list at a given position.Precondition:item is the value to be inserted;there is room in the array(mySize CAPACITY);and the position satisfies 0=pos=mySize.Postcondition:item has been inserted into the list at the position determined by pos(provided there is room and pos is a le
7、gal position).-*/void erase(int pos);/*-Remove a value from the list at a given position.Precondition:The list is not empty and the position satisfies 0=pos mySize.Postcondition:element at the position determined by pos has been removed(provided pos is a legal position).-*/*output*/void display(ostr
8、eam&out)const;/*-Display a list.Precondition:The ostream out is open.Postcondition:The list represented by this List object has been inserted into out.-*/private:/*Data Members*/int mySize;/current size of list stored in myArray ElementType myArrayCAPACITY;/array to store list elements;/-end of List
9、 class template/-Prototype of output operatortemplate ostream&operator(ostream&out,const List&aList);#endifChapter 9 99/-Definition of class constructortemplate inline List:List():mySize(0)/-Definition of empty()template inline bool List:empty()const return mySize=0;/-Definition of display()template
10、 inline void List:display(ostream&out)const for(int i=0;i mySize;i+)out myArrayi ;/-Definition of output operatortemplate inline ostream&operator(ostream&out,const List&aList)aList.display(out);return out;/-Definition of insert()template void List:insert(ElementType item,int pos)if(mySize=CAPACITY)c
11、err *No space for list element-terminating execution*n;exit(1);if(pos mySize)cerr *Illegal location to insert-pos pos;i-)myArrayi=myArrayi-1;/Now insert item at position pos and increase list size myArraypos=item;mySize+;Chapter 9 100/-Definition of erase()template void List:erase(int pos)if(mySize=
12、0)cerr *List is empty*n;return;if(pos=mySize)cerr Illegal location to delete-pos .List unchanged.*n;return;/Shift array elements left to close the gap for(int i=pos;i mySize;i+)myArrayi=myArrayi+1;/Decrease list size mySize-;8./*QueueT.h contains the declaration of class template Queue.Basic operati
13、ons:Constructor:Constructs an empty queue empty:Checks if a queue is empty enqueue:Modifies a queue by adding a value at the back front:Accesses the front queue value;leaves queue unchanged dequeue:Modifies a queue by removing the value at the front display:Displays the queue elements from front to
14、back Class Invariant:1.The queue elements(if any)are stored in consecutive positions in myArray,beginning at position myFront.2.0=myFront,myBack QUEUE_CAPACITY 3.Queues size QUEUE_CAPACITY-*/#include#ifndef QUEUET#define QUEUETconst int QUEUE_CAPACITY=128;template class Queue public:/*Function Membe
15、rs*/*Constructor*/Queue();/*-Construct a Queue object.Precondition:None.Chapter 9 101 Postcondition:An empty Queue object has been constructed;myFront and myBack are initialized to-1 and myArray is an array with QUEUE_CAPACITY elements of type QueueElement.-*/bool empty()const;/*-Check if queue is e
16、mpty.Precondition:None.Postcondition:True is returned if the queue is empty and false is returned otherwise.-*/void enqueue(const QueueElement&value);/*-Add a value to a queue.Precondition:value is to be added to this queue.Postcondition:value is added to back of queue provided there is space;otherw
17、ise,a queue-full message is displayed and execution is terminated.-*/void display(ostream&out)const;/*-Output the values stored in the queue.Precondition:ostream out is open.Postcondition:Queues contents,from front to back,have been output to out.-*/QueueElement front()const;/*-Retrieve value at fro
18、nt of queue(if any).Precondition:Queue is nonempty.Postcondition:Value at front of queue is returned,unless queue is empty;in that case,an error message is displayed and a garbage value is returned.-*/void dequeue();/*-Remove value at front of queue(if any).Precondition:Queue is nonempty.Postconditi
19、on:Value at front of queue has been removed,unless queue is empty;in that case,an error message is displayed and execution is terminated.-*/private:/*Data Members*/int myFront,myBack;QueueElement myArrayQUEUE_CAPACITY;Chapter 9 102;/end of class declaration/-Definition of Queue constructortemplate i
20、nline Queue:Queue():myFront(0),myBack(0)/-Definition of empty()template inline bool Queue:empty()const return(myFront=myBack);/-Definition of enqueue()template void Queue:enqueue(const QueueElement&value)int newBack=(myBack+1)%QUEUE_CAPACITY;if(newBack!=myFront)/queue isnt full myArraymyBack=value;m
21、yBack=newBack;else cerr *Queue full-cant add new value*n Must increase value of QUEUE_CAPACITY in Queue.hn;exit(1);/-Definition of display()template inline void Queue:display(ostream&out)const for(int i=myFront;i!=myBack;i=(i+1)%QUEUE_CAPACITY)out myArrayi ;cout endl;/-Definition of front()template
22、QueueElement Queue:front()const if(!empty()return(myArraymyFront);else cerr *Queue is empty-returning garbage value*n;QueueElement garbage;return garbage;Chapter 9 103/-Definition of dequeue()template void Queue:dequeue()if(!empty()myFront=(myFront+1)%QUEUE_CAPACITY;else cerr *Queue is empty-cant re
23、move a value*n;exit(1);#endif9./CartesianPoint.h#ifndef CARTESIANPOINT#define CARTESIANPOINT#include using namespace std;template class CartesianPoint public:CartesianPoint();CartesianPoint(CoordType xVal,CoordType yVal);CoordType getX()const;CoordType getY()const;void setX(CoordType xVal);void setY
24、(CoordType yVal);private:CoordType myX,myY;templateinline CartesianPoint:CartesianPoint():myX(0),myY(0)template inline CartesianPoint:CartesianPoint(CoordType xVal,CoordType yVal):myX(xVal),myY(yVal)template inline CoordType CartesianPoint:getX()const return myX;Chapter 9 104 template inline CoordTy
25、pe CartesianPoint:getY()const return myY;template inline void CartesianPoint:setX(CoordType xVal)myX=xVal;template inline void CartesianPoint:setY(CoordType yVal)myY=yVal;template inline ostream&operator(ostream&out,const CartesianPoint&point)out (point.getX(),point.getY();return out;template inline
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构与算法分析 第9章 答案 Larry Nyhoff 清华大学 数据结构 算法 分析
限制150内