哈希表实现大小调整功能
Hash Table implementing Resize function
我被分配了一项任务,调整哈希表的实现以允许动态调整大小。虽然我四处寻找线索和信息,但我仍然对哈希表的行为感到困惑,因此我应该如何调整大小。我被告知我需要添加一个大小调整函数,在插入和修改构造函数中调用它。我认为我已经做了正确的插入,似乎足够简单,但调整大小本身和构造函数是我正在努力。
编辑:所以我已经在调整大小函数,插入和构造函数工作,但我在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;
}
谢谢你的帮助!
目前,data
是固定大小的数组。你需要一个可变大小的存储
RecordType data[CAPACITY];
因此,data
需要是指针而不是固定数组,并且需要在构造函数中动态分配它。
当然,也改变CAPACITY
从一个常量到一个变量(每个表,所以不是static
-我会改变它的名字看起来不像一个宏/常量。
这位是"almost there":
table *newT;
newT = new table(newSize);
类型不应该是哈希表,而是RecordType
。
并且,正如注释所说,您需要将当前数据传输到新数据,然后使新表成为当前数据。最后,删除现在旧的数据。
编辑:我故意不为你写代码。你的任务是学习。大约20多年前,我编写了动态调整大小的哈希表,我认为我现在不需要练习它。你才是学习如何去做的人。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 为char数组调整zlib-zpipe
- 带内存和隔离功能的SQLite
- 调整大小后指向元素值的指针unordered_map有效?
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 类模板的成员功能的定义在单独的TU中完全专业化
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 在C++中调整向量中的索引
- 如何在C++中获得"静态纯虚拟"功能?
- 两个文件使用彼此的功能-如何解决
- 我应该实现右值推送功能吗?我应该使用std::move吗
- QML按钮点击功能执行顺序
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 有没有可能有一个只有ADL才能找到的非好友功能
- 功能样式转换从 'int' 到 'ItemType' 的匹配转换
- 文件系统:复制功能的速度秘诀是什么
- 英特尔 IPP 图像大小调整功能是否关心 alpha 值的位置
- 通过堆栈调整来thunk __ stdcall功能
- 哈希表实现大小调整功能