数据结构与算法分析 第9章 答案 Larry Nyhoff 清华大学出版社

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Chapter 9
/***** 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); /*-------------------------------------------------------------------Insert 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 legal 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(ostream & out) const; /*------------------------------------------------------------------Display a list. Precondition: The ostream out is open. Postcondition: The list represented byΒιβλιοθήκη Baiduthis List object has been inserted into out. ---------------------------------------------------------------------*/ private: /******** Data Members ********/ int mySize; ElementType myArray[CAPACITY]; }; //--- end of List class template //------ Prototype of output operator template <typename ElementType> ostream & operator<< (ostream & out, const List<ElementType> & aList); #endif
// current size of list stored in myArray // array to store list elements
– 98 –
Chapter 9
//--- Definition of class constructor template <typename ElementType> inline List<ElementType>::List() : mySize(0) {} //--- Definition of empty() template <typename ElementType> inline bool List<ElementType>::empty() const { return mySize == 0; } //--- Definition of display() template <typename ElementType> inline void List<ElementType>::display(ostream & out) const { for (int i = 0; i < mySize; i++) out << myArray[i] << " "; } //--- Definition of output operator template <typename ElementType> inline ostream & operator<<(ostream & out, const List<ElementType> & aList) { aList.display(out); return out; } //--- Definition of insert() template <typename ElementType> void List<ElementType>::insert(ElementType item, int pos) { if (mySize == CAPACITY) { cerr << "*** No space for list element -- terminating " "execution ***\n"; exit(1); } if (pos < 0 || pos > mySize) { cerr << "*** Illegal location to insert -- " << pos << ". List unchanged. ***\n"; return; } // First shift array elements right to make room for item for(int i = mySize; i > pos; i--) myArray[i] = myArray[i - 1]; // Now insert item at position pos and increase list size myArray[pos] = item; mySize++; }
4.
template <typename DataType> DataType arraySum(DataType x[], int length) { DataType sum = x[0]; for (int index = 1; index < length; index++) sum += x[index]; return sum; }
7.
/*-- ListT.h -----------------------------------------------------------This header file defines the data type List for processing lists. Basic operations are: Constructor empty: Check if list is empty insert: Insert an item erase: Remove an item display: Output the list <<: Output operator -----------------------------------------------------------------------*/ #include <iostream> #ifndef LISTT #define LISTT const int CAPACITY = 1024; template <typename ElementType> class List { public: /******** Function Members ********/ /***** Class constructor *****/ List(); /*--------------------------------------------------------------------Construct a List object. Precondition: None Postcondition: An empty List object has been constructed; mySize is 0. ---------------------------------------------------------------------*/ – 97 –
Chapter 9
Chapter 9: ADT Implementations: Templates and Standard Containers Exercises 9.3
1.
template <typename DataType> DataType average(DataType a, DataType b) { return (a + b)/2; }
– 96 –
Chapter 9
5.
template <typename DataType> void arrayMaxMin(DataType x[], int length, DataType & min, DataType & max) { min = max = x[0]; for (int i = 1; i < length; i++) { if (x[i] < min) min = x[i]; if (x[i] > max) max = x[i]; } }
6.
template <typename DataType> int search(DataType x[], int length, DataType target) { for (int index = 0; index < length; index++) if (x[index] == target) return index; return -1; } // index of -1 denotes not found
2.
template <typename DataType> DataType max(DataType a, DataType b) { if (a > b) return a; else return b;
3.
template <typename DataType> DataType median (DataType a, DataType b, DataType c) { DataType max = a, min = b; if (a < b) { max = b; min = a; } if (c > max) return max; else if (c < min) return min; else return c; }
相关文档
最新文档