哈希表的简单实现例子

Hash
// Hash.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include 
using namespace std;
enum {COUNT=17};
typedef int DATA ;
struct SNode
{
	DATA data;
	SNode* pNext;
};
//SNode* g_pHead = NULL; //单条链表
SNode* g_pData[COUNT] = {0}; //COUNT个链表的头
int g_nTest[COUNT]; //int个int
void SetAt(const DATA &data)
{
	int n = data%COUNT;//n ->  0-16
	SNode* p = new SNode;
	p ->data = data;
	//新节点的后继是某条链表的原来的头
	p ->pNext = g_pData[n];//g_pData[n]是一个指针类型SNode*
	g_pData[n] = p;//新的头节点
}
bool Lookup(const DATA &data)
{
	int n = data%COUNT;
	SNode* p = g_pData[n];
	while(p)
	{
		if(p->data == data)
			return true;
		p = p ->pNext;
	}
	return false;
}
int main(int argc, char* argv[])
{
	int i = g_nTest[5];//的类型是int
	SetAt(889);
	SetAt(1009);
	SetAt(1020);
	SetAt(1023);
	SetAt(1026);
	SetAt(1019);
	SetAt(1087);
	SetAt(1007);
	if(Lookup(1009))
		cout << "找到了" <
哈希表的简单实现例子_第1张图片

你可能感兴趣的:(c++)