简体   繁体   中英

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

I'm trying to create a spell checking program in C++ by reading in a dictionary from a .txt file. I've got the read in function working perfectly fine, the issue I'm coming across is when I try to navigate and add to my linked list.

When I try to set the pointer of the newest node to add, to the value of the head pointer, I'm getting an error stating No viable conversion from 'Node' to 'Node *'.

What is the best way to perform this conversion.

I've already tried turning my 'Node Head;' inside of my linked list class to a pointer but receive the same error.

To start I created my Node struct (Declared in a header file)

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; }

};

Followed by my LinkedList Class (Also declared in a Header file)

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);

};

Heres my Function

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);

}

The LinkedList class shouldn't own the first Node .

The member head should be a Node* width default value nullptr (the list is empty). listSize should also have a default value assigned.

LinkedList() head(nullptr), listSize(0) {};

Edit

Personally I would avoid to force the external code to manage the single nodes. Keep an implementation independent interface.

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);
};

In this way you centralize all list manipulation code into the LinkedList class.

You should also consider problems related to copying a LinkedList object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM