⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 seghash.cpp

📁 此代码运行于visual c++ 6.0的环境下
💻 CPP
字号:
// SegHash.cpp: implementation of the CSegHash class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SegHash.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

/*CSegHash::CSegHash()
{

}

CSegHash::~CSegHash()
{

}
*/
CSegHash::CSegHash(int length)
{
     M=length;
	 N=new node*[M];
	 for(int i=0;i<M;i++)
		N[i]=NULL;
}

CSegHash::CSegHash()
{  
     M=10000;
	 N=new node*[M];
	 for(int i=0;i<M;i++)
		N[i]=NULL;
}

void CSegHash::SetLength(int length)
{ 
   clear();
   delete[] N;
	M=length;
    N=new node*[M];
	 for(int i=0;i<M;i++)
		N[i]=NULL;     

}

CSegHash::~CSegHash()
{
   clear();
}
/////////////////////////////////////////////////////////////////////////
// Construction/Destruction   END
/////////////////////////////////////////////////////////////////////////
void CSegHash::Insert(const char* key)
{
	 if(Find(key))
	 	return;
      int index=ELFhash(key);
	  if(N[index]==NULL)
	  {
		  N[index]=new node;
		  strcpy(N[index]->key,key);
		  N[index]->link=NULL;
	  }
	  else
	  {
		  node* temp=new node;
		  temp->link=N[index];
		  strcpy(temp->key,key);
		  N[index]=temp;
	  } 
}
/////////////////////////////////////////////////////////////////////////
int CSegHash::ELFhash(const char* key)
{
	unsigned long h=0;
	while(*key)
	{
		h=( h << 4 ) + *key++;
		unsigned long g = h & 0xF0000000L;
		if(g) h ^= g >> 24;
		h &= ~g;
	}
	return h%M;
}
/////////////////////////////////////////////////////////////////////////////
bool CSegHash::Find(const char* key)
{
 	  int index=ELFhash(key);
	  for(node* temp=N[index];temp!=NULL;temp=temp->link)
	      if(strcmp(temp->key,key)==0)
			  return true;
	  return false;
}

void CSegHash::clear()
{
	for(int i=0;i<M;i++)
    	while(N[i]!=NULL)
		{
        	node* temp=N[i];
    	    N[i]=N[i]->link;
            delete temp;
		}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -