我正在尝试创建一个数据结构,将每个顶点存储在具有单独链接的哈希表中。这可以通过使用 vector 来存储每个顶点来轻松完成,但我想要恒定的时间搜索所以我试图使用哈希表来实现它。哈希表中的第一个节点称为 hashNode
,它将由一个 vertex
指针、一个 node
的 next
指针组成> 类型,以及一个名为 blockCount
的整数。 node
类型将由一个 vertex
指针和一个 node
指针组成。
我遇到的问题是我无法在我的 chainingTable
类中访问我的 vertex
类中的成员。当我尝试在我的 chainingTable
类中创建一个 vertex
对象或指针时,我收到错误消息说 vertex
未定义。我知道这是在说因为 chainingTable
不知道 vertex
类存在,但我在尝试修复它时遇到了很多麻烦。我尝试使用继承和 friend 关键字,但我认为我没有正确使用它们。任何帮助,将不胜感激。谢谢。
这是我目前拥有的代码。在 chainingHashTable.h 中,
#pragma once
using namespace std;
class chainingTable
{
class node;
public:
chainingTable(int size = 100)
:hashTable(size), currentSize(0) {}
struct hashNode
{
vertex * v;
node * next;
int blockCount;
};
struct node
{
vertex * v;
node * next;
};
void insert(vertex * v);
bool contains(vertex * v);
unsigned int getNumItems();
double averageSearchCost();
private:
unsigned int currentSize;
unsigned int hashFunc(string word);
vector<hashNode> hashTable;
};
unsigned int chainingTable::hashFunc(string word)
{
unsigned int sum = 0;
unsigned int pow = 1;
for (int i = 0; i < word.length(); i++)
{
sum = sum + word[i] * pow;
pow = pow * 26;
}
return sum % hashTable.size();
}
void chainingTable::insert(vertex * v)
{
}
directedWeightedGraph.h:
#pragma once
#include "chainingHashTable.h"
using namespace std;
class directedWeightedGraph
{
class edge;
class vertex
{
public:
string data;
list<edge> edgeList;
vertex(string x)
{
data = x;
}
};
class edge
{
public:
vertex * start;
vertex * end;
double weight;
edge(vertex * s, vertex * e, double w)
{
start = s;
end = e;
weight = w;
}
};
chainingTable vertexList;
public:
void addVertex(string v);
void addEdge(string s, string d, double w);
void display();
};
而且我知道使用 using namespace std
是不好的做法,但是这个程序只会被我使用,并且只是为了练习图形和哈希表。我喜欢更简洁的代码:]
最佳答案
首先,chainingHashTable.h
中没有#include directedWeightedGraph.h
。而这只是开始......
关于c++ - 尝试使用哈希表(使用链接)作为我的顶点列表来制作图形数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33511355/