通过自定义结构和链接列表类中的C 中的哈希表
Hash Table in C++ Through Custom Struct and Linked List Classes
我正在尝试通过从.txt文件中读取字典来创建C 中的拼写检查程序。我的读取功能的工作正常很好,我遇到的问题是当我尝试导航并添加到链接列表时。
当我尝试将最新节点的指针设置为添加的最新节点的指针时,我会遇到一个错误,说明没有可行的转换从"节点"到'node *'。
执行此转换的最佳方法是什么。
我已经尝试转动"节点头";在我的链接列表类的内部到指针,但会收到相同的错误。
开始我创建了我的节点结构(在标题文件中声明(
struct Node
{
private:
std::string word;
Node *nextNode;
public:
//Default constructor
Node();
~Node();
//My Setters and getters for the class
void setWord(std::string _word) { word = _word; }
std::string getWord() { return word; }
void setNode(Node *_nextNode) { nextNode = _nextNode; }
Node getNode() { return *nextNode; }
};
紧随其后的是我的linkedlist类(也在标题文件中声明(
class LinkedList
{
private:
Node head;
int listSize;
public:
LinkedList();
~LinkedList();
void setListSize(int _listSize) { listSize = _listSize; }
int getListSize() { return listSize; }
void setHead(Node _head) { head = _head; }
Node getHead() { return head; }
//Function that adds the next node to the head
void addToHead(LinkedList &myList, Node &myNode);
};
我的功能
void LinkedList::addToHead(LinkedList &myList, Node &myNode)
{
myNode.setNode(myList.getHead().getNode());
//Here is where I'm getting my error
//"No viable conversion from 'Node' to 'Node *'
myList.setHead(myNode);
}
LinkedList
类不应拥有第一个Node
。
成员head
应该是Node*
宽度默认值nullptr
(列表为空(。 listSize
还应分配一个默认值。
LinkedList() head(nullptr), listSize(0) {};
编辑
我个人会避免强迫外部代码来管理单个节点。保持实现独立接口。
class LinkedList
{
private:
Node *head_;
int size_;
public:
LinkedList();
~LinkedList();
int size() const { return listSize; }
// insert after the i-th element
void insert(std::size index, std::string const& word);
// return the i-th element
std::string &at(std::size index);
std::string const &at(std::size index) const;
// removes the i-th element
void remove(size::size index);
};
这样,您将所有列表操纵代码集中到LinkedList
类中。
您还应该考虑与复制LinkedList
对象有关的问题。
相关文章:
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 用C++将哈希表写入文件并从文件中恢复
- C++中的并发哈希表
- 在具有开放寻址的哈希表中插入节点 [优化逻辑]
- 与C++哈希表的基础知识混淆
- 调整大小和复制哈希表数组中的元素
- 带链接的基本哈希表
- C++哈希表中,两个相同的实现,但一个给出错误
- 如果索引不是整数,我们如何在 C++ 中插入哈希表
- 查找项目在哈希表中的位置
- 为什么C++ STL 哈希表 (unordered_map) 不接受向量作为键
- C++哈希表 - 如何解决自定义数据类型作为键的unordered_map冲突?
- 使用哈希表设置实现
- 同时写入和读取哈希表
- 在 C++ 中为特定哈希表创建插入函数
- 我可以比朴素哈希表更快地将随机字符串映射到两个类吗?
- 通过自定义结构和链接列表类中的C 中的哈希表
- 嗨,我正在尝试将集合的特定节点添加到列表中,该列表位于哈希表中
- 初始化哈希表的 stl 列表动态数组(单独链接)