我已经获得了调整哈希表实现的任务,以允许动态调整大小。虽然我一直在寻找线索和信息,但我仍然对哈希表的行为方式感到困惑,因此我应该如何调整大小。我被告知我需要添加一个resize函数,在insert中调用它并修改构造函数。我认为我已经完成了插入,看起来很简单,但调整大小本身和构造函数是我正在努力的。
编辑:所以我已经处理了resize函数,插入和构造函数,但是我在resize()的for循环中遇到错误,将旧数据插入到更大的表中,我不确定是什么现在出错了。有什么想法吗?以下是标题:
#ifndef TABLE1_H
#define TABLE1_H
#include <cstdlib> // Provides size_t
#include <cassert> // Provides assert
namespace main_savitch_12A
{
template <class RecordType>
class table
{
public:
size_t CAPACITY;
// CONSTRUCTOR
table( );
// MODIFICATION MEMBER FUNCTIONS
void insert(const RecordType& entry);
void remove(int key);
void resize( );
// CONSTANT MEMBER FUNCTIONS
bool is_present(int key) const;
void find(int key, bool& found, RecordType& result) const;
size_t size( ) const { return used; }
private:
// MEMBER CONSTANTS -- These are used in the key field of special records.
enum { NEVER_USED = -1 };
enum { PREVIOUSLY_USED = -2 };
// MEMBER VARIABLES
RecordType *data; //[CAPACITY];
size_t used;
// HELPER FUNCTIONS
size_t hash(int key) const;
size_t next_index(size_t index) const;
void find_index(int key, bool& found, size_t& index) const;
bool never_used(size_t index) const;
bool is_vacant(size_t index) const;
};
构造函数实现:
template <class RecordType>
table<RecordType>::table( )
{
CAPACITY = 30;
data = new RecordType[CAPACITY];
size_t i;
used = 0;
for (i = 0; i < CAPACITY; ++i)
data[i].key = NEVER_USED;
}
调整实施:
template <class RecordType>
void table<RecordType>::resize( )
{
RecordType *oldData = data;
int oldSize = CAPACITY;
CAPACITY *= CAPACITY;
//create a new table
RecordType *newData;
newData = new RecordType[CAPACITY];
size_t i;
used = 0;
for (i = 0; i < CAPACITY; i++)
newData[i].key = NEVER_USED;
//place data from old table to new, larger table.
for (int i = 0; i < oldSize; i++)
{
insert(newData[hash(i)]); //this is where I'm having trouble.
}
data = newData;
delete[] oldData;
}
插入实现:
template <class RecordType>
void table<RecordType>::insert(const RecordType& entry)
// Library facilities used: cassert
{
bool already_present; // True if entry.key is already in the table
size_t index; // data[index] is location for the new entry
assert(entry.key >= 0);
// Set index so that data[index] is the spot to place the new entry.
find_index(entry.key, already_present, index);
// If the key wasn't already there, then find the location for the new entry.
if (!already_present)
{
if (size( ) >= CAPACITY)
{
resize( ); // resize the table.
insert(entry); // reinsert entry into new table.
}
else if (size( ) < CAPACITY)
{
index = hash(entry.key);
while (!is_vacant(index))
index = next_index(index);
++used;
}
}
data[index] = entry;
size_t i;
for (i=0; i<CAPACITY; i++) cout << data[i].key << ' ';
cout << endl;
}
感谢您的帮助!
答案 0 :(得分:1)
目前,data
是一个固定大小的数组。您需要一个可变大小的存储空间
RecordType data[CAPACITY];
因此,data
需要是指针而不是固定数组,并且需要在构造函数中动态分配它。
当然,也将CAPACITY
从常量变为变量(每个表,所以不是static
- 我会将它的名称更改为看起来不像宏/常量的东西。
这个位“几乎就在那里”:
table *newT;
newT = new table(newSize);
该类型不应该是哈希表,而是RecordType
。
而且,正如评论所说,您需要将当前数据传输到新数据,然后将新表格作为当前数据。最后,删除现在的旧数据。
编辑:我故意不为你编写代码。你的任务是学习。我在20多年前写了动态可调整大小的哈希表,我认为我现在不需要练习它。你是学习如何做到这一点的人。