哈希表

文章目录

t1
#include<iostream>
using namespace std;

int* Hash;
int len, num;
int c = 0;

void insert(int x) {
	int index;
	c = 0;//探测上限
	index = x % 11;
	while (Hash[index] != 0&&c<=100) {
		index = (index + 1) % len;
		c++;
	}
	Hash[index] = x;
}

void find(int x,int &ok,int &time,int &index) {
	time = 0;
	index = x % 11;
	ok = 0;
	while (Hash[index] != x && Hash[index] != 0) {
		index = (index + 1) % len;
		time++;
	}
	if (Hash[index] == x) {
		ok = 1;
		index++;
	}
	else {
		ok = 0;
	}
	time++;
}

int main()
{
	int t;
	cin >> t;
	while (t--) {
		
		cin >> len >> num;
		Hash = new int[len];
		for (int i = 0; i < len; i++)Hash[i] = 0;
		for (int i = 0; i < num; i++) {
			int temp;
			cin >> temp;
			insert(temp);
		}
		for (int i = 0; i < len; i++) {
			if (i != 0) {
				if (Hash[i] != 0)cout << ' ' << Hash[i];
				else cout << " NULL";
			}
			else {
				if (Hash[i] != 0)cout << Hash[i];
				else cout << "NULL";
			}
		}
		cout << endl;
		int t1;
		cin >> t1;
		while (t1--) {
			int temp;
			cin >> temp;
			int ok,time, index;
			find(temp, ok, time, index);
			if (ok == 1)cout << 1 << ' ' << time << ' ' << index << endl;
			else cout << 0 << ' ' << time << endl;
		}
	}
	return 0;
}
t2
#include<iostream>
#include<cmath>
using namespace std;

int* Hash;
int len, num;
int c = 0;

void insert(int x) {
	int index;
	index = x % 11;
	int n = 1;
	int k = 1;
	int g = 1;
	int temp = index;
	while (Hash[temp] != 0 ) {
		if (n % 2 == 1) {
			k = pow(g, 2);
			g++;
		}
		else {
			k *= -1;
		}
		temp = index;
		temp = (temp + k+len) % len;
		n++;
	}
	index = temp;
	Hash[index] = x;
}

void find(int x, int& ok, int& time, int& index) {
	time = 0;
	index = x % 11;
	ok = 0;
	int n = 1;
	int k = 1;
	int g = 1;
	int temp = index;
	while (Hash[temp]!=x&&Hash[temp] != 0) {
		if (n % 2 == 1) {
			k = pow(g, 2);
			g++;
		}
		else {
			k *= -1;
		}
		temp = index;
		temp = (temp + k + len) % len;
		n++;
		time++;
	}
	index = temp;
	if (Hash[index] == x) {
		ok = 1;
		index++;
	}
	else {
		ok = 0;
	}
	time++;
}

int main()
{
	int t;
	cin >> t;
	while (t--) {

		cin >> len >> num;
		Hash = new int[len];
		for (int i = 0; i < len; i++)Hash[i] = 0;
		for (int i = 0; i < num; i++) {
			int temp;
			cin >> temp;
			insert(temp);
		}
		for (int i = 0; i < len; i++) {
			if (i != 0) {
				if (Hash[i] != 0)cout << ' ' << Hash[i];
				else cout << " NULL";
			}
			else {
				if (Hash[i] != 0)cout << Hash[i];
				else cout << "NULL";
			}
		}
		cout << endl;
		int t1;
		cin >> t1;
		while (t1--) {
			int temp;
			cin >> temp;
			int ok, time, index;
			find(temp, ok, time, index);
			if (ok == 1)cout << 1 << ' ' << time << ' ' << index << endl;
			else cout << 0 << ' ' << time << endl;
		}
	}
	return 0;
}
t3
#include<iostream>
#include<cmath>
using namespace std;

struct node
{
	int data;
	node* next;
	node() { next = NULL; }
	node(int data) { 
		this->data = data;
		next = NULL;
	}
};

node* Hash[11] = { NULL };

void insert(int x) {
	int index = x % 11;
	if (Hash[index] == NULL) {
		Hash[index] = new node(x);
	}
	else {
		node* p = new node(x);
		p->next = Hash[index];
		Hash[index] = p;
	}
}

void find(int x,int &index,int& time,int& ok) {
	index = x % 11;
	time = 1;
	for (node* p = Hash[index]; p != NULL; p = p->next) {
		if (p->data == x) {
			ok = 1; return;
		}
		time++;
	}
	node* p = new node(x);
	p->next = Hash[index];
	Hash[index] = p;
	ok = 0;
}

int main()
{
	int num;
	cin >> num;
	for (int i = 0; i < num; i++) {
		int temp;
		cin >> temp;
		insert(temp);
	}
	int t;
	cin >> t;
	while (t--) {
		int temp,index,time,ok;
		cin >> temp;
		find(temp,index,time,ok);
		if (ok == 0) {
			cout << "error" << endl;
		}
		else {
			cout << index << ' ' << time << endl;
		}
	}
	return 0;
}
t4
#include<iostream>
#include<cmath>
using namespace std;

struct node
{
	int data;
	node* next;
	node() { next = NULL; }
	node(int data) {
		this->data = data;
		next = NULL;
	}
};

node* Hash[11] = { NULL };

void insert(int x) {
	int index = x % 11;
	if (Hash[index] == NULL) {
		Hash[index] = new node(x);
	}
	else {
		node* q = Hash[index];
		while (q->next != NULL)q = q->next;
		node* p = new node(x);
		q->next = p;
	}
}

void find(int x, int& index, int& time, int& ok) {
	index = x % 11;
	time = 1;
	if (Hash[index] == NULL) {
		ok = 0;
		node* p = new node(x);
		Hash[index] = p;
		return;
	}
	//head not null
	for (node* p = Hash[index]; p != NULL; p = p->next) {
		if (p->data == x) {
			ok = 1;
			return;
		}
		time++;
	}
	node* q = Hash[index];
	while (q->next != NULL)q = q->next;
	node* p = new node(x);
	q->next = p;
	ok = 0;
}

int main()
{
	int num;
	cin >> num;
	for (int i = 0; i < num; i++) {
		int temp;
		cin >> temp;
		insert(temp);
	}
	int t;
	cin >> t;
	while (t--) {
		int temp, index, time, ok;
		cin >> temp;
		find(temp, index, time, ok);
		if (ok == 0) {
			cout << "error" << endl;
		}
		else {
			cout << index << ' ' << time << endl;
		}
	}
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值