开散列表(除留余数法)

算法课第二次作业

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const string file_path = "/Users/sjy/Documents/data.csv";
const int table_length = 200;
const int p = 199;

template <class Data1, class Data2>
struct Node{
	Data1 word;
	Data2 paraphrase;
	struct Node *next;
};

template <class Key, class Value>
class Hash{
	public:
		Hash();
		~Hash();
		bool load();
		void insertByKey(Key key, Value value);
		bool deleteByKey(Key key);
		Value queryBykey(Key key);
		void print();
	private:
		Node<Key,Value> *table[table_length];
		int getKey(Key str);
};

template <class Key, class Value>
Hash<Key, Value>::Hash(){
	for(int i = 0; i < table_length; i++){
		this->table[i] = new Node<Key, Value>;
		this->table[i]->next = NULL;
	}
}

template <class Key, class Value>
Hash<Key, Value>::~Hash(){
	for(int i = 0; i < table_length; i++){
		while(this->table[i] != NULL){
			Node<Key, Value> *temp = this->table[i];
			this->table[i] = this->table[i]->next;
			delete temp;
		}
	}
}

template <class Key, class Value>
bool Hash<Key, Value>::load(){
	ifstream fin;
	fin.open(file_path, ios::in);
	if(!fin.is_open()){
		return false;
	}
	string str;
	getline(fin, str);
	while(getline(fin, str)){
		int pos = str.find(',');
	    Key key = str.substr(0,pos);
	    Value value = str.substr(pos+1);
	    // cout<<key<<"  "<<value<<endl;
	    insertByKey(key, value);
	}
	return true;
}

template <class Key, class Value>
int Hash<Key, Value>::getKey(Key key){
	if(is_same<Key, string>::value){
		int ans = 0;
		for(int i = 0; i < key.size(); i++) {
			if(!(key[i] >= 'A' && key[i] <= 'Z' || key[i] >= 'a' && key[i] <= 'z')){
				return -1;
			}
			if(key[i] >= 'A' && key[i] <= 'Z'){
				key[i] = key[i] + 32;
			}
			ans += ((key[i] - 'a') + 1) % p;
		}
		return (ans % p);
	}else {
		if(key.size()==1){
			return (key[0] % p);
		}else{
			int ans = 0;
			for(int i = 0; i < key.size(); i++) {
				ans += (key[i] - '0')*10 % p;
			}
			return ans;
		}
	}
}

template <class Key, class Value>
void Hash<Key, Value>::insertByKey(Key key,Value value){
	int hash_key = getKey(key);
	if(hash_key == -1){
		return;
	}
	Node<Key, Value> *node = new Node<Key, Value>;
	node->word = key;
	node->paraphrase = value;
	node->next = table[hash_key]->next;
	table[hash_key]->next = node;
}

template <class Key, class Value>
bool Hash<Key, Value>::deleteByKey(Key key){
	int hash_key = getKey(key);
	bool flag = false;
	Node<Key, Value> *temp = table[hash_key];
	while(temp->next != NULL){
		if(temp->next->word == key){
			flag = true;
			Node<Key, Value> *t = temp->next;
			temp->next = temp->next->next;
			delete t;
			t = NULL;
		}else{
			temp = temp->next;
		}
	}
	return flag;
}

template <class Key, class Value>
Value Hash<Key, Value>::queryBykey(Key key){
	int hash_key = getKey(key), pos = 0, step = 1;
	Node<Key, Value> *temp = table[hash_key];
	Value value = "";
	while(temp->next != NULL){
		if(temp->next->word == key){
			pos = step;
			value = temp->next->paraphrase;
			break;
		}
		temp = temp->next;
		step++;
	}
	return value;
}

template <class Key, class Value>
void Hash<Key, Value>::print(){
	for(int i = 0; i < table_length; i++){
		if(table[i]->next != NULL){
			cout<<i<<" : ";
			Node<Key, Value> *temp = table[i];
			while(temp->next){
				cout<<temp->next->word<<"  ";
				temp = temp->next;
			}
			cout<<endl;
		}
	}
}

void test(Hash<string, string> *hash){
	string key;
	cin>>key;
	string pos = hash->queryBykey(key);
	if(pos!="") {
		cout<<pos<<endl;
	}else{
		cout<<"暂未收录该词"<<endl;
	}
	// if(hash->deleteByKey(key)){
	// 	cout<<"成功删除"<<endl;
	// }else{
	// 	cout<<"暂未收录该词"<<endl;
	// }
	// pos = hash->queryBykey(key);
	// if(pos!="") {
	// 	cout<<pos<<endl;
	// }else{
	// 	cout<<"暂未收录该词"<<endl;
	// }
}

int main(){
	cout<<"数据加载中..."<<endl;
    Hash<string, string> hash;
    if(hash.load()){
    	cout<<"数据加载完成!"<<endl;
    	// hash.print();
    	test(&hash);
    	// hash.print();
    }else{
    	cout<<"文件打开失败,请检查路径是否正确!"<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值