通过自定义结构和链接列表类中的C 中的哈希表

Hash Table in C++ Through Custom Struct and Linked List Classes

本文关键字:哈希表 列表 链接 自定义 结构      更新时间:2023-10-16

我正在尝试通过从.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对象有关的问题。