4020-基于链地址法的散列表的插入(C++,附思路以及头插法,尾插法两种代码)

描述

请写出在散列表中插入关键字为k的一个记录的算法,设散列函数为H,H(key)=key%13,解决冲突的方法为链地址法。

输入

多组数据,每组三行,第一行为待输入的关键字的个数n,第二行为对应的n个关键字,第三行为需要插入的关键字k。当n=0时输入结束。

输出

每组数据输出用链地址法处理冲突的散列表。

输入样例 1 

5
1 4 2 3 5
6
4
2 5 8 15
18
0

输出样例 1

0
1 1
2 2
3 3
4 4
5 5
6 6
7
8
9
10
11
12
0
1
2 2 15
3
4
5 5 18
6
7
8 8
9
10
11
12

思路:
这个题目中要求的哈希表和邻接表的处理方法非常类似,所以如果你还没有掌握邻接表的话,推荐先去做邻接表部分的题。同时,在我的这篇文章中,借助4003这道题比较详细地讲解了邻接表的插入,初始化等操作:

(15条消息) 4003基于邻接表的新顶点的增加(C++,附详细解析)_鹤天寻的博客-CSDN博客icon-default.png?t=LA92https://blog.csdn.net/qq_54416938/article/details/121582300?spm=1001.2014.3001.5501所以在以下思路中,默认你已经会了邻接表的相关操作了~

哈希散列的思想类似于之前循环队列中的思想,或者是计组中的组相联映射,都是通过对数字进行取余,从而判定它们的位置。

题目里说mod13,所以我们建立一个长度为13的链表,每个表头的序号就是遍历时的序号,初始化令指针域指向NULL。

接着是插入操作。你可以观察到,题目中给的插入新节点操作方法与我们插入老结点的方法没有区别,通过输入方式可以看出来,它们可以用同一种插入方式解决。所以简化一下,新节点在插入的时候一并实现,所以控制循环次数是i<=n。那么这个数字究竟要映射到什么地方呢?就是下标为temp%13对应的位置,是通过对13的余数判定插入位置的。

头插法代码不讲了,在上面文章里有;说下尾插法:就是新设置一个节点,从当前位置出发一直遍历到那一行的结尾,然后再使用尾插法就可以了。

两种方法代码如下:

头插法(虽然oj判定用的并不是头插法)

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<stack>
#include<set>
#include<map>
using namespace std;

typedef struct LNode
{
	int data;
	struct LNode* next;
}*linklist, LNode;
void Create(linklist& vl,int n)
{
	vl = new LNode[13];
	for (int i = 0; i < 13; i++)
	{
		vl[i].data = i;
		vl[i].next = NULL;
	}
	for (int i = 0; i <= n; i++)
	{
		int temp;
		cin >> temp;
		linklist p = new LNode;
		p->data = temp;
		p->next = vl[temp % 13].next;
		vl[temp % 13].next = p;
	}
}
void Show(linklist vl,int n)
{
	for (int i = 0; i < 13; i++)
	{
		linklist p = vl[i].next;
		cout << i;
		while (p)
		{
			cout << ' ';
			cout << p->data;
			p = p->next;
		}
		cout << endl;
	}
}
int main()
{
	int n;
	while (cin >> n && n != 0)
	{
		linklist vl;
		Create(vl, n);
		Show(vl, n);
	}
	return 0;
}

尾插法(通过的代码):

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<stack>
#include<set>
#include<map>
using namespace std;

typedef struct LNode
{
	int data;
	struct LNode* next;
}*linklist, LNode;
void Create(linklist& vl,int n)
{
	vl = new LNode[13];
	for (int i = 0; i < 13; i++)
	{
		vl[i].data = i;
		vl[i].next = NULL;
	}
	for (int i = 0; i <= n; i++)
	{
		int temp;
		cin >> temp;
		linklist p = &vl[temp%13], q = new LNode;
		while (p&&p->next)
			p = p->next;
		q->data = temp;
		q->next = NULL;
		p->next = q;
	}
}
void Show(linklist vl,int n)
{
	for (int i = 0; i < 13; i++)
	{
		linklist p = vl[i].next;
		cout << i;
		while (p)
		{
			cout << ' ';
			cout << p->data;
			p = p->next;
		}
		cout << endl;
	}
}
int main()
{
	int n;
	while (cin >> n && n != 0)
	{
		linklist vl;
		Create(vl, n);
		Show(vl, n);
	}
	return 0;
}

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值