key.h

来自「链表和应用包括单链表和双链表等等。自己写的」· C头文件 代码 · 共 109 行

H
109
字号
#ifndef KEY_H
#define KEY_H

#include "utility.h"
#include "doubly_link.h"


//Definition of a Key class:

class Key {
public:
	static int comparisons;
	Key(int x = 0);
	int the_key() const;
	//Add any constructors methods for key data.
private:
	int key;
	//Add declaration of key data members here.

	
};


Key::comparisons = 0;
int Key::the_key() const 
{
    return key;
}

// Declare overloaded comparison operators for keys.
bool operator == (const Key &x, const Key &y);
bool operator >  (const Key &x, const Key &y);
bool operator <  (const Key &x, const Key &y);
bool operator >= (const Key &x, const Key &y);
bool operator <= (const Key &x, const Key &y);
bool operator != (const Key &x, const Key &y);

bool operator ==(const Key &x, const Key &y) 
{
	
	Key::comparisons++;
	return x.the_key() == y.the_key();
}

bool operator >(const Key &x, const Key &y) 
{
	
	Key::comparisons++;
	return x.the_key() > y.the_key();
}

bool operator <(const Key &x, const Key &y) 
{
	
	Key::comparisons++;
	return x.the_key() < y.the_key();
}
bool operator >=(const Key &x, const Key &y) 
{
	
	Key::comparisons++;
	return x.the_key() >= y.the_key();
}

bool operator <=(const Key &x, const Key &y) 
{
	
	Key::comparisons++;
	return x.the_key() <= y.the_key();
}

bool operator !=(const Key &x, const Key &y) 
{
	
	Key::comparisons++;
	return x.the_key() != y.the_key();
}



// Definition of a Record class:
class Record {
public:
	operator Key();                //implicit conversion from Record to Key.
	//Add any constructors and methods for Record objects.
private:
	//Add data components.
};


Error_code sequential_search(const List<Record> &the_list, const Key &target, int &position)
/*Post: If the entry in the_list has key equal to target, then return success and the 
        output parameter position locates such an entry within the list.
		Otherwise return not_present and position becomes invalid.*/
{

	int s = the_list.size();
	for(position = 0; position<s; position++) {
		Record data;
		the_list.retrieve(position,data);
		if(data = target) return success;
	}
           return not_present;
}




#endif 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?