![](/img/trans.png)
[英]Create a separate chaining hash table contain words derived from a txt file
[英]How to add info from txt file to hash table
如何實現addInfo函數。 我們必須使用鏈接哈希函數根據序列號查找和排序行。 但在我需要做addInfo之前。 例如:第一行“1009 1”Royal Queen“2015 160”序列號為1009,如果有其他船只的序列號以9結尾,我們必須一起顯示。 這是我到目前為止的代碼:
#include <iostream>
#include <fstream>
using namespace std;
struct ShipRecord
{
int serialNum;
int shipType;
string name;
int year;
int cap;
ShipRecord *next;
ShipRecord(int x)
{
serialNum = x;
next = nullptr;
}
};
const int SIZE = 10;
class HashMgr
{
ShipRecord *head = nullptr;
ShipRecord *tail = nullptr;
public:
HashMgr()
{
ShipRecord * hashTable[SIZE] = {nullptr};
string line;
ifstream myfile;
myfile.open("shipRecords.txt");
if(myfile.is_open())
{
while (!myfile.eof())
{
getline (myfile, line);
addInfo(line);
}
myfile.close();
}
}
addInfo(string line)
{}
displayOne(int serialNum)
{}
displayAll()
{}
int main()
{
HashMgr hm;
hm.displayAll();
hm.displayOne(1009);
hm.displayOne(1010);
hm.displayOne(1019);
hm.displayOne(1029);
hm.displayOne(1039);
hm.displayOne(1014);
hm.displayOne(1008); /// Prompt a message to that the record does not exist
hm.deleteOne(1009);
hm.deleteOne(1039);
hm.displayAll();
return 0;
}
/*
Here is the txt.file
1009 1 "Royal Queen" 2015 160
1010 2 "Carnival" 2016 1600
1019 1 "Ocean King" 2013 110
1029 2 "Royal Prince" 2012 2000
1039 2 "Royal Princess" 2010 2100
1014 2 "Royal Caribbean" 2016 1600
*/
我建議你應該使用帶有哈希函數的密鑰創建Hashtable作為數字%10,值應該是ShipRecord *但是指向vector。
在迭代文件時,繼續根據哈希函數評估添加新元素以校正向量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.