简体   繁体   中英

C++ hashtable using an array of head pointers. How do i assign my addNode() private method to my head pointer key position?

I have previously built a hashtable using an array of pointers to linked list (separate class) objects, and now am trying to build a hashtable using an array of head pointers that calls its own linked list private methods. Calling the private linked list methods is not the problem. Assigning them to its key position in the array of head pointers is where I am stuck.

without adding my entire code, here is my basic hashtable.h file:

#define HASHTABLESIZE 15;

class Hashtable {
    public:
        Hashtable();

        bool addEntry(int, string);

    private:
        int hash(id);
        Node *head;

        bool addNode(int, string);
}

and here is my hashtable.cpp file:

Hashtable::Hashtable() {
    this->head = new Node[HASHTABLESIZE];
}

int Hashtable::hash(int id) {
    if (id > 0) {
        return id % HASHTABLESIZE;
    }
}

bool Hashtable::addEntry(int id, string stringInfo) {
    bool inserted = false;
    int position = hash(id);

    if (id > 0 && stringInfo != "") {
        head[position] = addNode(id, stringInfo); // I know this doesn't work (this is where I am stuck)
        inserted = true;
    }
}

bool Hashtable::addNode(int id, string stringInfo) {
    bool addedNode = false;

    if (id > 0 && stringInfo != "") {
        Node *newNode = new Node;
        Node *current = head;
        newNode->prev = NULL;
        newNode->next = NULL;

        if (head != NULL) {
            ...
            ...
            ...
        }
    }
}

with the linkedlist class I could just head[position].addNode(id, "string") , but within its own class I cannot figure out how to assign the specific key and call the private method to only assign the values to its corresponding index within the array of head pointers. Any help is appreciated.

Hope it works. If doesn't it'll atleast give you some direction.

Hashtable::Hashtable()
{
    //make a new array of Node pointers
    //why not instances of Nodes, this makes it easier, as you see later.
    this->head = new Node*[HASHTABLESIZE];
    for (int i = 0; i < HASHTABLESIZE; i++){
        head[i] = nullptr; //set each equal to a nullptr
    }
}

int Hashtable::hash(int id)
{
    if (id > 0)
    {
        return id % HASHTABLESIZE;
    }
}

bool Hashtable::addEntry(int id, string stringInfo)
{
    bool inserted = false;
    int position = hash(id);

    if (id > 0 && stringInfo != "")
    {
        addNode(head[position], id, stringInfo); //basically we add a new Node to the Node that was put in.
        inserted = true;
    }
}

//we have to be able to actually change the pointer value, thus we give it a reference to a pointer.
bool Hashtable::addNode(Node *&toAddTo, int id, string stringInfo)
{
    //new node
    Node *toAdd = new Node();

    //Dont know your Node struct, so just assuming
    toAdd->value = stringInfo;
    toAdd->id = id;

    //next node is nullptr, so if you're later iterating through this list you know when you've hit the end.
    toAdd->next = nullptr;

    //heres why it's easier to have a list of pointers
    if (toAddTo != nullptr){
        //make them point to each other.
        toAddTo->next = toAdd;
        toAdd->prev = toAddTo;
    } else {
        toAdd->prev = nullptr //same as why next is a nullptr
    }

    //now the head at that position will be pointing to the last element you put in
    toAddTo = toAdd;
    return true;
}

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