数据结构_8:查找:Hash:C++

c++版本


#include<iostream>
#include<stdlib.h>
//#include "xcept.h"
using namespace std;

// use when zero was expected
class BadInput {
   public:
      BadInput() {}
};

// insufficient memory
class NoMem {
   public:
      NoMem() {}
};



template<class E, class K>
class HashTable {
   public:
      HashTable(int divisor = 11); 
      ~HashTable() {delete [] ht;
                    delete [] empty;}
      bool Search(const K& k, E& e) const;
      HashTable<E,K>& Insert(const E& e);
      void Output();// output the hash table
   private:
      int hSearch(const K& k) const;
      int D; // hash function divisor
      E *ht; // hash table array
      bool *empty; // 1D array
};

template<class E, class K>
HashTable<E,K>::HashTable(int divisor)
{// Constructor.
   D = divisor;

   // allocate hash table arrays
   ht = new E [D];
   empty = new bool [D];

   // set all buckets to empty
   for (int i = 0; i < D; i++)
      empty[i] = true;
}

template<class E, class K>
int HashTable<E,K>::hSearch(const K& k) const
{// Search an open addressed table.
 // Return location of k if present.
 // Otherwise return insert point if there is space.
   int i = k % D; // home bucket
   int j = i;     // start at home bucket
   do {
      if (empty[j] || ht[j] == k) return j;
      j = (j + 1) % D;  // next bucket
      } while (j != i); // returned to home?

   return j;  // table full
}

template<class E, class K>
bool HashTable<E,K>::Search(const K& k, E& e) const
{// Put element that matches k in e.
 // Return false if no match.
   int b = hSearch(k);
   if (empty[b] || ht[b] != k) return false;
   e = ht[b];
   return true;
}

template<class E, class K>
HashTable<E,K>& HashTable<E,K>::Insert(const E& e)
{// Hash table insert.
   K k = e; // extract key
   int b = hSearch(k);

   // check if insert is to be done
   if (empty[b]) {empty[b] = false;
                  ht[b] = e;
                  return *this;}

   // no insert, check if duplicate or full
   if (ht[b] == k) throw BadInput(); // duplicate
   throw NoMem(); // table full
   return *this;  // Visual C++ needs this line
}

template<class E, class K>
void HashTable<E,K>::Output()
{
   for (int i = 0; i< D; i++) {
      if (empty[i]) cout << "empty" << endl;
      else cout << ht[i] << endl;}
}

class element {
   friend void main(void);
   public:
      operator long() const {return key;}
   private:
      int data;
      long key;
};

void main(void)
{
   HashTable<element, long> h(11);
   element e;
   e.key = 80;
   h.Insert(e);
   e.key = 40;
   h.Insert(e);
   e.key = 65;
   h.Insert(e);
   h.Output();
   e.key = 58;
   h.Insert(e);
   e.key = 24;
   h.Insert(e);
   cout << ' ' << endl;
   h.Output();
   e.key = 2;
   h.Insert(e);
   e.key = 13;
   h.Insert(e);
   e.key = 46;
   h.Insert(e);
   e.key = 16;
   h.Insert(e);
   e.key = 7;
   h.Insert(e);
   e.key = 21;
   h.Insert(e);
   cout << ' ' << endl;
   h.Output();
   e.key = 99;
   try {h.Insert(e);}
   catch (NoMem)
      {cout  << " No memory for 99" << endl;}
}

Inhabitable(链表散列)

// file Chash.h
#ifndef ChainedHashTable_
#define ChainedHashTable_

#include <iostream.h>
#include <stdlib.h>
#include "sochain.h" // sorted chain

template<class E, class K>
class ChainHashTable {
   public:
      ChainHashTable(int divisor = 11)
         {D = divisor;
          ht = new SortedChain<E,K> [D];}
      ~ChainHashTable() {delete [] ht;}
      bool Search(const K& k, E& e) const
           {return ht[k % D].Search(k, e);}
      ChainHashTable<E,K>& Insert(const E& e)
           {ht[e % D].DistinctInsert(e);
            return *this;}
      ChainHashTable<E,K>& Delete(const K& k, E& e)
           {ht[k % D].Delete(k, e);
            return *this;}
      void Output() const;   // output the table
   private:
      int D;                 // divisor
      SortedChain<E,K> *ht;  // array of chains
};

template<class E, class K>
void ChainHashTable<E,K>::Output() const
{
   SortedChain<E,K> c;
   for (int i = 0; i < D; i++) {
     cout << "Chain " << i << ' ';
     ht[i].Output(cout);
     cout << endl;}
}

#endif

// test chained hash table

#include <iostream.h>
#include "chash.h"

class element {
   friend void main(void);
   public:
      operator long() const {return key;}
      element& operator =(long y)
      {key = y; return *this;}
   private:
      int data;
      long key;
   };

ChainHashTable<element, long> h(11);
element e;

void main(void)
{
   e.key = 80;
   h.Insert(e);
   e.key = 40;
   h.Insert(e);
   e.key = 65;
   h.Insert(e);
   e.key = 58;
   h.Insert(e);
   e.key = 24;
   h.Insert(e);
   e.key = 2;
   h.Insert(e);
   e.key = 13;
   h.Insert(e);
   e.key = 46;
   h.Insert(e);
   e.key = 16;
   h.Insert(e);
   e.key = 7;
   h.Insert(e);
   e.key = 21;
   h.Insert(e);
   h.Delete(2,e);
   h.Delete(16,e);
   h.Delete(24,e);
   h.Delete(80,e);
   h.Output();
}


Hash_STL

http://www.cnblogs.com/CheeseZH/p/5176970.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值