哈希表添加和打印功能问题

Hash Table add and print function issues

本文关键字:功能 问题 打印 添加 哈希表      更新时间:2023-10-16

所以我有一个赋值,我必须创建一个哈希表。正如标题所说,我对addItemprintTable函数有问题。我很难弄清楚出了什么问题。任何帮助都会很棒。感谢

HashTable.h

#ifndef HASHTABLE_H_
#define HASTABLE_H_
#include "stdafx.h"
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
class HashTable {
public:
    HashTable();
    ~HashTable();
    int hash(string key, const int TABLE_SIZE); // Returns the hash value for the given key, the ASCII values of each character % the table size
    void addItem(string title, string author, int isbn); // Inserts a new item into the table, calls has function on the key title to determine the correct bucket
    int numItemsAtIndex(int index); //Helper function to printTable, counts # of items in each bucket
    void printTable(); //Prints first item of each bucket, includes number of items stored at that bucket
private:
    struct Node
    {
        string title;
        string author;
        int isbn;
        Node* next;
        Node() : title(""), author(""), isbn(0), next(NULL) {}
        Node(string ntitle, string nauthor, int nisbn) : title(ntitle), author(nauthor), isbn(nisbn), next(NULL) {}
    };
    typedef struct Node* Nodeptr;
    static const int TABLE_SIZE = 10;
    Nodeptr Table[TABLE_SIZE];
};
#endif /*HASHTABLE_H_*/

HashTable.cpp

#include "stdafx.h"
#include <iostream>
#include "HashTable.h"
#include <cstdlib>
using namespace std;
HashTable::HashTable()
{
    for (int i = 0; i < TABLE_SIZE; i++)
        Table[i] = new Node;
}
HashTable::~HashTable() {}
int HashTable::hash(string key, const int TABLE_SIZE)
{
    int index, sum = 0;
    for (int i = 0; i < key.length(); i++)
        sum += key[i];
    index = sum % TABLE_SIZE;
    return index;
}
void HashTable::addItem(string title, string author, int isbn)
{
    Nodeptr newNode = new Node(title, author, isbn);
    int index = hash(title, TABLE_SIZE);
    if (index == 0)
        Table[index] = newNode;
    else
    {
        Nodeptr tempNode = new Node;
        tempNode = Table[0];
        while (tempNode->next != NULL)
            tempNode = tempNode->next;
        newNode = tempNode->next;
    }
}
int HashTable::numItemsAtIndex(int index)
{
    Nodeptr temp = Table[0];
    int counter = 0;
    if (temp->title == "")
        return counter;
    else
        while (temp != NULL)
        {
            counter++;
            temp = temp->next;
        }
    return counter;
}
void HashTable::printTable()
{
    Nodeptr temp = new Node;
    temp = Table[0];
    cout << "Book Hash Table:" << endl;
    cout << "--------------------------" << endl;
    for (int x = 0; x <= TABLE_SIZE; x++)
    {
        Table[x];
        cout << "Index: "  << x << endl;
        cout << "Title: " << temp->title << endl;
        cout << "Author: " << temp->author << endl;
        cout << "ISBN: " << temp->isbn << endl;
        cout << "Number of Values at this Index: "  << endl;
        cout << endl;
    }
    delete temp;
}

HashTest.cpp

#include "stdafx.h"
#include "HashTable.h"
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    HashTable table;
    table.addItem("Pride and Prejudice", "Jane Austen", 12345678);
    table.addItem("Contact", "Carl Sagan", 9999333939);
    table.addItem("The Hunger Games", "Suzanne Collins", 12388888);
    table.addItem("Harry Potter", "J.K. Rowlings", 55555678);
    table.addItem("The Man in the High Castle", "Philip K Dick", 9595959595);
    table.addItem("Bleak House", "Charles Dickens", 47389023849);
    table.printTable();
    return 0;
}

您似乎不了解指针是如何工作的,当您使用new创建指针时,必须使用delete释放它,否则会出现内存泄漏。此外,您还需要了解intlonglong long之间的区别,isbn变量存储了大量的数字,因此需要将其声明为long long。此外,您还需要有一种方法来确定Table数组中的一个项是否已设置,例如,您可以将所有项初始化为NULL

我修复了你的代码,希望它能帮助你理解你做错了什么:

哈希表.hpp

    struct Node
    {
        string title;
        string author;
        long long isbn;
        Node* next;
        Node() : title(""), author(""), isbn(0), next(NULL) {}
        Node (string ntitle, string nauthor, long long nisbn) : title(ntitle), author(nauthor), isbn(nisbn), next(NULL) {}
    };
    void addItem(string title, string author, long long isbn); 

哈希表.cpp

HashTable::HashTable()
{
    for (int i = 0; i < TABLE_SIZE; i++) Table[i] = NULL;
}
HashTable::~HashTable()
{
    Nodeptr cur, temp;
    for (size_t i = 0; i < TABLE_SIZE; i++)
    {
        if (Table[i] != NULL)
        {
            cur = Table[i];
            while (cur != NULL)
            {
                temp = cur;
                cur = cur->next;
                delete (temp);
            }
        }
    }
}
int HashTable::hash(string key, const int TABLE_SIZE)
{
    int index, sum = 0;
    for (int i = 0; i < key.length(); i++) sum += key[i];
    index = sum % TABLE_SIZE;
    return index;
}
void HashTable::addItem(string title, string author, long long isbn)
{
    Nodeptr newNode = new Node (title, author, isbn);
    int index = hash (title, TABLE_SIZE);
    if (Table[index] == NULL) Table[index] = newNode;
    else
    {
        Nodeptr tempNode = Table[index];
        while (tempNode->next != NULL) tempNode = tempNode->next;
        tempNode->next = newNode;
    }
}
int HashTable::numItemsAtIndex (int index)
{
    Nodeptr temp = Table[index];
    int counter = 0;
    if (temp == NULL) return counter;
    else
    {
        while (temp != NULL)
        {
            counter++;
            temp = temp->next;
        }
    }
    return counter;
}
void HashTable::printTable()
{
    Nodeptr temp;
    cout << "Book Hash Table:" << endl;
    cout << "--------------------------" << endl;
    for (int x = 0; x < TABLE_SIZE; x++)
    {
        temp = Table[x];
        if (temp != NULL)
        {
            cout << "Index: "  << x << endl;
            cout << "Title: " << temp->title << endl;
            cout << "Author: " << temp->author << endl;
            cout << "ISBN: " << temp->isbn << endl;
            cout << "Number of Values at this Index: " << numItemsAtIndex (x);
            cout << endl << endl;
        }
    }
}

哈希测试.cpp

int main()
{
    HashTable table;
    table.addItem("Pride and Prejudice", "Jane Austen", 12345678L);
    table.addItem("Contact", "Carl Sagan", 9999333939);
    table.addItem("The Hunger Games", "Suzanne Collins", 12388888L);
    table.addItem("Harry Potter", "J.K. Rowlings", 55555678L);
    table.addItem("The Man in the High Castle", "Philip K Dick", 9595959595L);
    table.addItem("Bleak House", "Charles Dickens", 47389023849L);
    table.printTable();
    return 0;
}

结果

Book Hash Table:
--------------------------
Index: 3
Title: The Hunger Games
Author: Suzanne Collins
ISBN: 12388888
Number of Values at this Index: 1
Index: 4
Title: Pride and Prejudice
Author: Jane Austen
ISBN: 12345678
Number of Values at this Index: 1
Index: 6
Title: Contact
Author: Carl Sagan
ISBN: 9999333939
Number of Values at this Index: 1
Index: 7
Title: The Man in the High Castle
Author: Philip K Dick
ISBN: 9595959595
Number of Values at this Index: 2
Index: 8
Title: Harry Potter
Author: J.K. Rowlings
ISBN: 55555678
Number of Values at this Index: 1

更多的是评论,但我发现空间不足。

您的代码中有很多奇怪的语句。

Nodeptr temp = new Node;
temp = Table[0];

和在printTable

Table[x];

您需要决定碰撞策略。你的Node表示你想要一个链表作为你的bucket,但addItem成员没有解决这个问题。

为什么index = 0addItem中特别?

if (index == 0)
    Table[index] = newNode;

你需要重新思考每个成员需要完成什么。

HashTable::HashTable()
{
    for (int i = 0; i < TABLE_SIZE; i++)
        Table[i] = NULL;
}
void HashTable::addItem(string title, string author, int isbn)
{
    Nodeptr newNode = new Node(title, author, isbn);
    int index = hash(title, TABLE_SIZE);
    if (Table[index] == NULL)
        Table[index] = newNode;
    else
    {
        Nodeptr tempNode = Table[index];
        while (tempNode->next != NULL)
            tempNode = tempNode->next;
        tempNode = newNode->next;
    }
}
int HashTable::numItemsAtIndex(int index)
{
    if ((index < 0) || (index >= TABLE_SIZE)) return 0;
    Nodeptr temp = Table[index];
    int counter = 0;
        while (temp != NULL)
        {
            counter++;
            temp = temp->next;
        }
    return counter;
}

void HashTable::printTable()
{
    cout << "Book Hash Table:" << endl;
    cout << "--------------------------" << endl;
    for (int x = 0; x < TABLE_SIZE; x++)
    {
        if (Table[x] == NULL) continue;
        NodePtr temp = Table[x];
        do
        {
        cout << "Index: "  << x << endl;
        cout << "Title: " << temp->title << endl;
        cout << "Author: " << temp->author << endl;
        cout << "ISBN: " << temp->isbn << endl;
        cout << "Number of Values at this Index: "  << endl;
        cout << endl;
        temp = temp->next;
        } while (temp != NULL);
    }
}