当前位置:编程学习 > 网站相关 >>

c++哈希表的制作

/***************************************************************************
 *  xcl 2012.2.18   *
 *  HashTable       *
 *  说明:本哈希表采用线性散列处理冲突,哈希函数为素数取模运算*
 ***************************************************************************/
 
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
 
#include <iostream>
#include <cstdlib>
#include <time.h>
 
 
using namespace std;
 
#define add_scope 11    //地址范围
int hash(int key)    //处理键值
{
    return (key*3)%11;
}
 
typedef int KeyType,ValType,StatusType; //定义类型
//实际数据结构-----------------------------
typedef struct{
 
    KeyType    key;
   
    ValType val;
   
    StatusType stat;
       
}Element;
 
//实现类------------------------------------
class MyList
{
    public:
    MyList(int n);
    ~MyList();
   
    public:
    void SetKey(int key,int val);
    void DeleteKey(int key);
    int SearchKey(int key);
    int GetMapSize();
    void ShowList();
   
    private:
    Element *elem;
        int     hash_size;
        int     dataNum;
};
 
//-----------------------------------------
MyList::MyList(int n)
{
    hash_size = n;
    elem = new Element();
    dataNum = 0;
    for(int i=0;i < n;i++)
        {
            elem[i].key = 0;
            elem[i].val = 0;
            elem[i].stat = 0;
        }
}
 
//------------------------------------------
MyList::~MyList()
{
    delete[] elem;
}
 
//------------------------------------------
void MyList::SetKey(int key,int val)
{
    int pos;
    if(hash_size==dataNum)
    {
        cout << "OverFlow~~~~" <<endl;
    }
    pos=hash(key);   
    for(int i=0;i<hash_size;i++)
    {
        if(elem[pos].stat == 0)
        {
            elem[pos].key=key;
            elem[pos].val=val;
            elem[pos].stat=1;
            dataNum++;           
            cout << "KeySet Success!  "<<elem[pos].key<<"\t"<<elem[pos].val<<"\t"<<pos <<endl;
            return;
        }
        else
        {
            pos=(pos+1)%hash_size;
        }
    }
    cout << "Insert Key Failed~~" <<endl;
   
}
 
//--------------------------------------------
int MyList::SearchKey(int key)
{
    int pos = hash(key);
    int stat = elem[pos].stat;
    int num=1;   
    while(stat!=0 && num<dataNum)
    {
        if(elem[pos].key == key)
        {           
            return pos;
        }
        else
        {
            pos=(pos+1)%hash_size;
            stat=elem[pos].stat;
            num++;
        }   
    }   
    return hash_size;    //防止冲突
}
 
//---------------------------------------------
void MyList::DeleteKey(int key)
{   
    int pos=SearchKey(key);
    if(pos==hash_size)
    {
        cout << "Cannot found the key! Please check out your insert!" << endl;
    }
    else
    {       
        elem[pos].key=0;
        elem[pos].val=0;
        elem[pos].stat=0;
        dataNum--;
        cout << "Delete Success~~" <<endl;       
    }
}
 
//-------------------------------------------------
int MyList::GetMapSize()
{   
    return dataNum%hash_size;
}
 
//------------------------------------------------
void MyList::ShowList()
{
    if(dataNum == 0)
    {
            cout<<"The Table is NULL"<<endl;
            return;
    }
    cout << "There are "<< dataNum <<" nums in the HashTable!" << endl;
    cout<<"************HashTable****************"<<endl;
    cout<<"key             value          position"<<endl;
    for(int i = 0; i<hash_size;i++)
    {
            i
补充:综合编程 , 安全编程 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,