索引结构与散列技术 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;
}

原创粉丝点击