我目前正在编写一个程序,该程序使用哈希表搜索船舶记录。它基于一个名为“ shipRecords.txt”的文本文件,包含以下内容:
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
我遇到的麻烦是以下面的预期输出所示的格式显示记录。
显示由3个功能组成:displayAll(),displayOne()和deleteone()。
displayAll()-对于每个存储桶,它首先显示一个存储桶编号,然后显示 列出存储桶中的所有记录。系统将在 列表。
displayOne()-使用给定的序列号,存储区编号和 显示船。
deleteone()-删除给定序列号的记录。
我以前从未使用过哈希表,但是对于C ++来说仍然是陌生的,但是如果有人可以帮助我或给我一些提示以实现预期的输出,我将非常感谢!
正在进行测试代码...
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;
struct ShipRecord
{
int serialNum;
int shipType;
string name;
int year;
int cap;
ShipRecord* link;
};
const int SIZE = 10;
class HashMgr
{
ShipRecord* hashTable[SIZE] = {nullptr};
public:
HashMgr()
{
string line;
ifstream inputFile;
inputFile.open("shipRecords.txt");
if(inputFile.is_open())
{
while (!inputFile.eof())
{
getline(inputFile,line);
addInfo(line);
}
inputFile.close();
}
}
~HashMgr()
{
/// To DO: Add your code here to ensure no memory leak occurs
for (int i = 0; i < SIZE; ++i)
{
while (hashTable[i] != nullptr)
{
ShipRecord* tempRecord = hashTable[i];
hashTable[i] = tempRecord->link;
delete tempRecord;
}
}
}
HashFunc(int serialNum)
{
return serialNum % SIZE;
}
void addInfo(string line)
{
vector<string> tokens;
stringstream check1(line);
string inter;
cout << endl << "PARSING STRING:\"" << line << "\"" << endl;
cout << "---------------"<< endl;
while(getline(check1,inter,'\"'))
{
tokens.push_back(inter);
}
for(unsigned int i = 0; i < tokens.size(); i++)
cout << tokens[i] << endl;
cout << "---------------"<< endl;
}
void displayOne(int serialNum)
{
int bucket = HashFunc(serialNum);
ShipRecord* tempRecord = hashTable[bucket];
while(tempRecord != nullptr && tempRecord->serialNum != serialNum)
{
tempRecord = tempRecord->link;
}
if(tempRecord == nullptr)
{
cout << serialNum << " <- This ship record does not exist." << endl;
}
else if(tempRecord->serialNum == serialNum)
{
cout << tempRecord->serialNum << setw(10)
<< tempRecord->shipType << setw(10)
<< tempRecord->name << setw(10)
<< tempRecord->year << setw(10)
<< tempRecord->cap << setw(10) << endl;
}
}
void displayAll()
{
for(int i = 0; i < SIZE; i++)
{
ShipRecord* tempRecord = hashTable[i];
while(tempRecord != nullptr)
{
cout << tempRecord->serialNum << setw(10)
<< tempRecord->shipType << setw(10)
<< tempRecord->name << setw(10)
<< tempRecord->year << setw(10)
<< tempRecord->cap << setw(10) << endl;
tempRecord = tempRecord->link;
}
}
}
void deleteone(int serialNum)
{
cout << "Ship record " << serialNum << " deleted!" << endl;
int bucket = HashFunc(serialNum);
ShipRecord* tempRecord = hashTable[bucket];
ShipRecord* link;
while(tempRecord != nullptr && tempRecord->serialNum != serialNum)
{
link = tempRecord->link;
free(tempRecord);
tempRecord = link;
}
}
};
int main()
{
HashMgr hm;
cout << "displayAll()" << endl << endl;
hm.displayAll();
cout << "displayOne()" << endl << endl;
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);
cout << "displayAll()" << endl << endl;
hm.displayAll();
return 0;
}
当前输出
PARSING STRING: 1009 1 "Royal Queen" 2015 160
PARSING STRING: 1010 2 "Carnival" 2016 1600
PARSING STRING: 1019 1 "Ocean King" 2013 110
PARSING STRING: 1029 2 "Royal Prince" 2012 2000
PARSING STRING: 1039 2 "Royal Princess" 2010 2100
PARSING STRING: 1014 2 "Royal Caribbean" 2016 1600
displayAll()
1010 2 Carnival 2016 160
1014 2 Royal Caribbean 2016 160
1009 1 Royal Queen 2015 16
displayOne()
1009 1 Royal Queen 2015 16
1010 2 Carnival 2016 160
1019 <- This ship record does not exist.
1029 <- This ship record does not exist.
1039 <- This ship record does not exist.
1014 2 Royal Caribbean 2016 160
1008 <- This ship record does not exist.
displayAll()
1010 2 Carnival 2016 160
1014 2 Royal Caribbean 2016 160
1009 1 Royal Queen 2015 16
预期产量
PARSING STRING:" 1009 1 "Royal Queen" 2015 160"
---------------
1009
1
Royal Queen
2015
160
---------------
PARSING STRING:" 1010 2 "Carnival" 2016 1600"
---------------
1010
2
Carnival
2016
1600
---------------
PARSING STRING:" 1019 1 "Ocean King" 2013 110"
---------------
1019
1
Ocean King
2013
110
---------------
PARSING STRING:" 1029 2 "Royal Prince" 2012 2000"
---------------
1029
2
Royal Prince
2012
2000
---------------
PARSING STRING:" 1039 2 "Royal Princess" 2010 2100"
---------------
1039
2
Royal Princess
2010
2100
---------------
PARSING STRING:" 1014 2 "Royal Caribbean" 2016 1600"
---------------
1014
2
Royal Caribbean
2016
1600
---------------
displayAll()
Bucket #0
1010 2 "Carnival" 2016 1600
Bucket #4
1014 2 "Royal Caribbean" 2016 1600
Bucket #9
1009 1 "Royal Queen" 2015 160
1019 1 "Ocean King" 2013 110
1029 2 "Royal Prince" 2012 2000
1039 2 "Royal Princess" 2010 2100
displayOne()
Bucket #0
1010
Bucket #4
1014
Bucket #8
1008 <- This record does not exist!
Bucket #9
1009
1019
1029
1039
displayAll()
Bucket #0
1010 2 "Carnival" 2016 1600
Bucket #4
1014 2 "Royal Caribbean" 2016 1600
Bucket #9
1019 1 "Ocean King" 2013 110
1029 2 "Royal Prince" 2012 2000
Deleted ship record (1009)!
Deleted ship record (1039)!