所以我有一个任务,我必须创建一个哈希表。正如标题所说,我遇到了addItem
和printTable
函数的问题。我无法弄清楚出了什么问题。任何帮助都会很棒。感谢
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;
}
答案 0 :(得分:2)
你似乎不明白指针是如何工作的,当你使用new
来创建一个指针时,你必须用delete
释放它,否则你就会有内存泄漏。您还需要了解int
,long
和long long
之间的区别,isbn
变量存储大量数据,因此您需要将其声明为long long
。
此外,您还需要了解Table
数组中的项是否已设置,例如,您可以将所有项初始化为NULL
。
我修复了你的代码,希望你能帮助你理解你做错了什么:
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);
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;
}
}
}
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
答案 1 :(得分:0)
更多评论,但我发现空间不足。
你的代码中有很多奇怪的陈述。
Nodeptr temp = new Node;
temp = Table[0];
并在printTable
Table[x];
您需要确定碰撞策略。您的Node
表示您希望链接列表作为您的存储分区,但addItem
成员无法解决此问题。
index = 0
为什么addItem
特殊?
if (index == 0)
Table[index] = newNode;
您需要重新思考每个成员需要完成的任务。
答案 2 :(得分:0)
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);
}
}