索引结构与散列技术 POWERBY CHenCHengNET
来源:互联网 发布:数据库insert into 编辑:程序博客网 时间:2024/06/06 01:31
// hashtable.h: interface for the hashtable class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_HASHTABLE_H__B98B72DB_0601_4B42_B259_59E8CBB1C59C__INCLUDED_)
#define AFX_HASHTABLE_H__B98B72DB_0601_4B42_B259_59E8CBB1C59C__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class hashtable
{
public:
hashtable ();
hashtable (int key);
virtual ~hashtable ();
hashtable* InserttheNode (hashtable* new_node);
bool HaveDeletetheNode (int key);
hashtable* SearchtheNode (int key);
int HashingFunction (int key);
bool IsExistedintheHashtable (int key);
void DisplaytheHashTable ();
private:
int m_key;
hashtable* m_next;
hashtable* m_priot;
};
#endif // !defined(AFX_HASHTABLE_H__B98B72DB_0601_4B42_B259_59E8CBB1C59C__INCLUDED_)
// hashtable.cpp: implementation of the hashtable class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "hashtable.h"
#include <iostream>
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
static hashtable* s_hashtableArray[20];
hashtable::hashtable()
{
m_next = NULL;
m_priot = NULL;
}
hashtable::hashtable(int key)
{
m_key = key;
m_next = NULL;
m_priot = NULL;
}
hashtable::~hashtable()
{
}
///////////////////////////////////////////////////////////////////////
//memberfunction
///////////////////////////////////////////////////////////////////////
hashtable* hashtable::InserttheNode(hashtable* new_node)
{
int array_position = HashingFunction(new_node->m_key);
if(IsExistedintheHashtable(new_node->m_key))
{
cout << " The node you input is existed in the hash-table" << endl;
return NULL;
}
else
{
if(s_hashtableArray[array_position]!=NULL)
{
s_hashtableArray[array_position]->m_priot = new_node;
}
new_node->m_next = s_hashtableArray[array_position];
s_hashtableArray[array_position] = new_node;
cout << " The node you input has been inserted into the hash-table" << endl;
return new_node;
}
}
bool hashtable::HaveDeletetheNode(int key)
{
int array_position = HashingFunction(key);
hashtable* p = SearchtheNode(key);
if(p==NULL)
{
cout << " The node you input is not existed in the hash-table" << endl;
cout << "Please check your input and try again" << endl;
return false;
}
else
{
if(p->m_priot==NULL)
{
s_hashtableArray[array_position] = p->m_next;
}
else if(p->m_next==NULL)
{
p->m_priot->m_next = NULL;
}
else
{
p->m_priot->m_next = p->m_next;
p->m_next->m_priot = p->m_priot;
}
delete p;
return true;
}
}
hashtable* hashtable::SearchtheNode(int key)
{
int array_position = HashingFunction(key);
hashtable* p = s_hashtableArray[array_position];
while(p!=NULL)
{
if(p->m_key==key)
{
return p;
}
p = p->m_next;
}
return NULL;
}
int hashtable::HashingFunction(int key)
{
return (key%19);
}
bool hashtable::IsExistedintheHashtable(int key)
{
int array_position = HashingFunction(key);
hashtable* p = s_hashtableArray[array_position];
while(p!=NULL)
{
if(p->m_key==key)
{
return true;
}
p = p->m_next;
}
return false;
}
void hashtable::DisplaytheHashTable()
{
hashtable* p = NULL;
for(int i=0; i<20; i++)
{
p = s_hashtableArray[i];
while(p!=NULL)
{
cout << p->m_key << " ";
p = p->m_next;
}
cout << endl;
}
}
// chenchengnet_索引结构与散列技术.cpp : Defines the entry point for the console application.
//本程序用C++类编写而成
#include "stdafx.h"
#include "hashtable.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
hashtable* m_p = NULL;
for(int i=1; i<51; i++)
{
m_p = new hashtable(i);
m_p->InserttheNode(m_p);
}
cout << "新建立的散列表: " << endl;
m_p->DisplaytheHashTable();
cout << "删除2,5, 12, 15, 26, 28六个结点后的散列表: " << endl;
m_p->HaveDeletetheNode(2);
m_p->HaveDeletetheNode(5);
m_p->HaveDeletetheNode(12);
m_p->HaveDeletetheNode(15);
m_p->HaveDeletetheNode(26);
m_p->HaveDeletetheNode(28);
m_p->DisplaytheHashTable();
return 0;
}
- 索引结构与散列技术 POWERBY CHenCHengNET
- 索引与散列
- 11索引与散列
- 数据在计算机中存储的物理结构有四种:顺序、链表、散列与索引
- 数据存储结构---顺序 链式 索引 散列
- 散列与Java Map结构分析
- 散列函数与层级结构数据
- 聚簇、非聚簇索引结构及B树、位图、散列索引
- 数据库与索引结构
- 多列索引结构和原理
- 多列索引结构和原理
- mysql 单列索引与多列索引
- 索引和散列
- 索引和散列
- 数据库开发(8)索引与散列
- MySQL数据库索引中的单列索引与多列索引
- MySQL数据库索引中的单列索引与多列索引
- 四种数据存储结构---顺序存储 链接存储 索引存储 散列存储
- 软件测试工程师面试问题
- 公布《编程的奥秘》配套升级版《.NET 2.0面向对象程序设计(VB2005篇)》写作构想及全书目录
- Swing是MVC设计的典范
- 用JDOM建立XML文件
- [手持设备大作业]之前的工作
- 索引结构与散列技术 POWERBY CHenCHengNET
- 平遥印象
- 所有漂亮的代码跑哪里去了?
- 避免Java EE项目评估中的常见错误
- 利用正则表达式限制网页表单里的文本框输入内容
- jsp技巧大全
- Java程序性能优化 !
- 感觉真正考研复习的第一天
- 我对大赛题目的理解