I'm currently writing a program that uses hash tables searching through ship records. It's based off a text file I named "shipRecords.txt" containing the following:
1 2 3 4 5 6
|
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
|
The thing I'm having trouble with is displaying the records in the format as shown in the expected output below.
The displays are consisted of in 3 functions: displayAll(), displayOne(), and deleteOne().
displayAll()- For each bucket, it first displays a bucket number and then
lists all the records in the bucket. The system will display all records in a
listing.
displayOne()- With a given serial number, the bucket # and information of
the ship is displayed.
deleteOne()- Delete the record of the given serial number.
I have never used hash tables before, but if anyone could help me out or give me some hints to achieve the expected output, I would really appreciate it!
Code in progress...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
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* temp = hashTable[i];
hashTable[i] = temp->link;
delete temp;
}
}
}
addInfo(string line)
{
//int i = 0;
//size_t p1 = 0;
size_t p2 = 0;
string temp = line;
cout << endl << "PARSING STRING:\"" << line << "\"" << endl;
cout << "---------------" << endl;
while(temp.length() > 5)
{
if(temp[0] == ' ')
{
temp = temp.substr(1);
}
else if(temp[0] == '"')
{
p2 = temp.find_first_of('"', 1);
cout << temp.substr(1, p2 - 1) << endl;
temp = temp.substr(p2 + 1);
}
else
{
p2 = temp.find_first_of(' ');
cout << temp.substr(0, p2) << endl;
temp = temp.substr(p2);
}
}
cout << "---------------" << endl << endl;
}
displayOne(int serialNum)
{
/*
for(int i = 0; i < SIZE; i++)
{
if(SIZE == serialNum)
{
cout << serialNum << endl;
}
}
cout << serialNum << " <- This record does not exist!" << endl;
*/
}
displayAll()
{
}
deleteOne(int serialNum)
{
/*
for(int i = 0; i < SIZE; i++)
{
delete hashTable[i];
}
cout << serialNum << " <- Records deleted!" << endl;
*/
}
};
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
//cout << "deleteOne()" << endl << endl;
hm.deleteOne(1009);
hm.deleteOne(1039);
cout << "displayAll()" << endl << endl;
hm.displayAll();
return 0;
}
|
Current Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
PARSING STRING:" 1009 1 "Royal Queen" 2015 160"
***********
1009
1
Royal Queen
2015
***********
PARSING STRING:" 1010 2 "Carnival" 2016 1600"
***********
1010
2
Carnival
2016
***********
PARSING STRING:" 1019 1 "Ocean King" 2013 110"
***********
1019
1
Ocean King
2013
***********
PARSING STRING:" 1029 2 "Royal Prince" 2012 2000"
***********
1029
2
Royal Prince
2012
***********
PARSING STRING:" 1039 2 "Royal Princess" 2010 2100"
***********
1039
2
Royal Princess
2010
***********
PARSING STRING:" 1014 2 "Royal Caribbean" 2016 1600"
***********
1014
2
Royal Caribbean
2016
***********
displayAll()
displayOne()
displayAll()
|
Expected Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
PARSING STRING:" 1009 1 "Royal Queen" 2015 160"
***********
1009
1
Royal Queen
2015
***********
PARSING STRING:" 1010 2 "Carnival" 2016 1600"
***********
1010
2
Carnival
2016
***********
PARSING STRING:" 1019 1 "Ocean King" 2013 110"
***********
1019
1
Ocean King
2013
***********
PARSING STRING:" 1029 2 "Royal Prince" 2012 2000"
***********
1029
2
Royal Prince
2012
***********
PARSING STRING:" 1039 2 "Royal Princess" 2010 2100"
***********
1039
2
Royal Princess
2010
***********
PARSING STRING:" 1014 2 "Royal Caribbean" 2016 1600"
***********
1014
2
Royal Caribbean
2016
***********
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)!
|