mardi 30 avril 2019

How to add info from txt file to hash table

How to implement the addInfo function. We have to find and sort the lines based on their serial number using chaining hash function. But before I need to do addInfo. For example: the first line "1009 1 "Royal Queen" 2015 160" the serial number is 1009 and if there is other ships that have the serial number ended with 9 we have to display them together. Here is the code that I have so far:

#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
*/

Aucun commentaire:

Enregistrer un commentaire