associated keys. Examples of the conversion operation: A method of the class Record with the declaration operator Key( const CA constructor for the class Key with declaration Key const Record & C If the classes Key and record are identical, no conversion needs to be defined since any record is automatically a Key. We do not assume that a record has a Key object as a data member, although often it does. We merely assume that the compiler can turn a record into its corresponding ey
associated keys. ◆Examples of the conversion operation: ◆A method of the class Record, with the declaration operator Key( ) const; ◆A constructor for the class Key, with declaration Key(const Record &); ◆If the classes Key and Record are identical, no conversion needs to be defined, since any Record is automatically a Key. ◆We do not assume that a Record has a Key object as a data member, although often it does. We merely assume that the compiler can turn a Record into its corresponding Key
Parameters for Search Functions Each searching function has two input parameters First is the list to be searched Second is the target key for which we are searching. Each searching function will also have an output parameter and a returned value: The returned value has type Error code and indicates whether or not the search is successful in finding an entry with the target key. If the search is successful, then the returned value is success,and the output parameter called position will locate the target within the list
Parameters for Search Functions Each searching function has two input parameters: ◆First is the list to be searched; ◆Second is the target key for which we are searching. Each searching function will also have an output parameter and a returned value: ◆The returned value has type Error_code and indicates whether or not the search is successful in finding an entry with the target key. ◆If the search is successful, then the returned value is success,and the output parameter called position will locate the target within the list
If the search is unsuccessful, then the value not present is returned, and the output parameter may have an undefined vallue or a value that will differ from one method to another. Key Definition in C++ To select existing types as records and keys a client could use type definition statements such as: typedef int Key; typedef int Record; a client can design new classes that display appropriate behavior based on the following skeletons:
◆If the search is unsuccessful, then the value not present is returned,and the output parameter may have an undefined value or a value that will differ from one method to another. Key Definition in C++ To select existing types as records and keys, a client could use type definition statements such as: typedef int Key; typedef int Record; A client can design new classes that display appropriate behavior based on the following skeletons:
Definition of 在本章我们使用的类声明如下 a Key class class Key static data number i public: static int comparisons: 由于只关心查找key Keylint x=o)i key =x; y 不关心 record中的 int the key ( )const i return key; y 其它内容,因此将 Record定义为key Key& operator=(const int x) i key =x; return *this; y private int ke y char other: typedef Key record
class Key { public: static int comparisons; Key(int x=0) { key = x; } int the_key( ) const { return key; } Key& operator=(const int x) { key = x; return *this; } private: int key; char *other; }; 在本章我们使用的类声明如下: Definition of a Key class typedef Key Record; 由于只关心查找key, 不关心Record中的 其它内容,因此将 Record定义为key static data number
7.2 Sequential Search 1. Algorithm and procedure Begin at one end of the list and scan down it until the desired key is found or the other end is reached: 我们在第6章中实现的fnd方法: int Find(const List entry &x); ∥/查找元素x,找不到返回-1,否则为x在表中的位置 就是顺序查找方法,与本节书上的方法区别仅仅是: 返回了类型是 Error code,位置由引用参数带回
7.2 Sequential Search 1. Algorithm and procedure Begin at one end of the list and scan down it until the desired key is found or the other end is reached: 我们在第6章中实现的find方法: int Find(const List_entry &x); // 查找元素x,找不到返回-1,否则为x在表中的位置 就是顺序查找方法,与本节书上的方法区别仅仅是: 返回了类型是Error_code,位置由引用参数带回