hashtable print()函数c ++

时间:2013-04-12 21:33:12

标签: c++ hashtable

这仍然是我的家庭作业。我的作业最后一个问题。我一直在为我的print()编写代码。我无法完全生成我想要的输出。使用我的print(),我无法使用insert函数输出添加到哈希表中的值。我知道在我的print()中我使用了错误的List数组,但我似乎无法弄清楚如何将我的List数组合并到我的print()中。这是我的类声明,以及我的insert()和print()。

const int EMPTY = 0;
const int OCCUPIED = 1;
class HTable
{
  public:
HTable(int size);
void insert( const string &key);
void remove(const string &key);
void print();


  private:
vector<list<string>> List;
int currSize;

//int hash(string key)const;
unsigned hash(const std::string& key) const;
int hash_string( const string &s );
int hashFunction(string key);
int HTableSize;
//int *status_arr;
ostream &   operator <<( ostream &);
HTable * table;
int *tableContents;
int recordSpace;
};

刀片()

inline void HTable::insert(  const string &key )
{
  // hash the key to determine which bucket the key would be stored in.
  int bucketNum = hash(key) ;

  list<string>& bucket = List[bucketNum] ;

  // search the bucket to see if the key is already in the bucket.
  if ( bucket.empty() || bucket.end() == find(bucket.begin(), bucket.end(), key) )
    bucket.push_back(key) ;           // if it's not in the bucket, store it in the bucket.
}

打印()

inline void HTable::print()
{
    list<string>::iterator itr;
for(int i = 0; i < HTableSize; i++)
{
    if(tableContents[i] != OCCUPIED)
        cout << (i)   << endl;

}

}

0 个答案:

没有答案