DS哈希查找--链地址法

题目描述
给出一个数据序列,建立哈希表,采用求余法作为哈希函数,模数为11,哈希冲突用链地址法和表头插入

如果首次查找失败,就把数据插入到相应的位置中

实现哈希查找功能

输入

第一行输入n,表示有n个数据 第二行输入n个数据,都是自然数且互不相同,数据之间用空格隔开 第三行输入t,表示要查找t个数据
从第四行起,每行输入一个要查找的数据,都是正整数

输出

每行输出对应数据的查找结果,每个结果表示为数据所在的位置[0-11)和查找次数,中间用空格分开。

样例输入

6

11 23 39 48 75 62

6

39

52

52

63

63

52

样例输出

6 1

error

8 1

error

8 1

8 2
提示
注意,当两次输入要相同的查找数据,如果第一次查找不成功就会执行插入,那么第二次查找必然成功,且查找次数为1次(因为做表头插入)

例如示例数据中输入两次52,第一次查找失败就把52插入到位置8,第二次查找就成功了,所以第一次输出error,第二次就输出8 1

为什么第三次输入52会输出8 2

为什么第三次输入52会输出8 2

因为使用的是头插法,52排在了63后面

#include <iostream>
using namespace std;

class Node {
	public:
		int data;
		Node *next;
		Node(int n) {
			data = n;
			next = NULL;
		}
		Node() {
			data = 0;
			next = NULL;
		}
		Node* insert(int n) { //表头插入
			Node *p = new Node(n);
			p->next = this;
			return p;
		}
		int search(int find) {
			Node *p = this;
			int time = 0;
			while(p) {
				time++; //记录查找次数
				if(p->data == find) {
					return time;
				}
				p = p->next;
			}
			return -1; //查找未成功
		}
		~Node() {
			Node *p = this;
			Node *q;
			while(p) {
				q = p->next;
				delete []p;
				p = q;
			}
		}
};

class HashTable {
	private:
		Node **root;
		int size;
	public:
		HashTable(int s) {
			size = s;
			root = new Node*[size];
			for(int i; i < size; i++) {
				root[i] = new Node(0);
			}
		}
		int Hash(int n) {
			return n % 11;
		}
		void insert(int n) {
			int hash = Hash(n);
			root[hash] = root[hash]->insert(n);
		}
		void search(int find) {
			int hash = Hash(find);
			if(root[hash]->search(find) > -1) {
				cout<<hash<<" "<<root[hash]->search(find)<<"\n";
			} else {
				cout<<"error\n";
				insert(find);
			}
			return ;
		}
		~HashTable() {
			delete []root;
		}
};

int main() {
	int n;
	HashTable h(11);
	cin>>n;
	while(n--) {
		int data;
		cin>>data;
		h.insert(data);
	}
	int t;
	cin>>t;
	while(t--) {
		int find;
		cin>>find;
		h.search(find);
	}
	return 0;
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
#include #include typedef struct node { int data; struct node *next; }node; init_hash(node **A,int n) { int i; for(i=0;idata=0; A[i]->next=NULL; } } insert_hash(node **A,int value,int n) { int key; node *p,*q; key=value%n; if(A[key]->next!=NULL) { p=A[key]->next; while(p->next!=NULL) p=p->next; q=(node *)malloc(sizeof(node)); q->data=value; q->next=NULL; p->next=q; } else { q=(node *)malloc(sizeof(node)); q->data=value; q->next=NULL; A[key]->next=q; } } int search_hash(node **A,int value,int n) { int key; node *p; key=value%n; if(A[key]->next==NULL) return 0; else { p=A[key]->next; while(p!=NULL) { if(p->data==value) return 1; } return 0; } } delete_hash(node **A,int value,int n) { int key; node *p,*q; key=value%n; p=A[key]; q=A[key]->next; while(q->data!=value) { p=q; q=q->next; } p->next=q->next; free(q); } print_hash(node **A,int n) { int i; node *p; for(i=0;inext!=NULL) { p=A[i]->next; while(p!=NULL) { printf("%d ",p->data); p=p->next; } } } printf("\n"); } main() { int i,n,value,Case; node **A; printf("输入待排序元素个数:\n"); scanf("%d",&n); A=(node **)malloc(sizeof(node*)*n); //申请一个指针型数组A[n] init_hash(A,n);//初始化数组A printf("输入hash表的值(空格键分开):\n"); for(i=0;i<n;i++) //建hash表 { scanf("%d",&value); insert_hash(A,value,n); } printf("请选择hash表的操作\n1.查询\t2.插入\t3.删除\t4.输出\n"); scanf("%d",&Case); switch(Case) { case 1: { printf("请输入要查询的元素:"); scanf("%d",&value); printf(search_hash(A,value,n)?"Success!\n":"Failure!\n"); //查询 } case 2: { printf("请输入要插入的元素:"); scanf("%d",&value); insert_hash(A,value,n); //插入 } case 3: { printf("请输入要删除的元素:"); scanf("%d",&value); printf(search_hash(A,value,n)?"删除成功!\n":"表中不存在该元素!\n"); delete_hash(A,value,n); } case 4: //输出 { printf("表中元素为:\n"); print_hash(A,n); } } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值