【问题标题】:Hash Tables: Ship Records哈希表:船舶记录
【发布时间】:2019-11-21 19:30:10
【问题描述】:

我目前正在编写一个使用哈希表搜索船舶记录的程序。它基于我命名为“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)!

【问题讨论】:

  • displayAll: ShipRecord * hashTable = nullptr; while (hashTable != nullptr) ..为什么你希望这个函数能打印任何东西?
  • 我以前从未使用过哈希表,对 C++ 还是很陌生, -- std::unordered_map 是 C++ 中的哈希表。
  • @foreknownas_463035818;老实说,我不知道自己在做什么,目前我正在尝试各种方法来让我的程序正常运行。
  • @PaulMcKenzie;我会试一试。
  • 基本上,您必须提供哈希函数,但我在您的代码中没有看到。还有read this

标签: c++ hashtable


【解决方案1】:

有几件事不起作用:

  1. 函数 displayAll 不会做任何事情,因为 hashtable 变量是由 nullptr 初始化的,其余的应该在 hashptr 不是 nullptr 时工作

  2. 函数 displayOne 将始终打印“记录不存在”,因为没有其他方法可以退出该函数。如果您的 serialNum 小于 SIZE(常量 10),并且您传递的参数高于 1000,则不会打印任何其他内容。

  3. 我不确定 C++ 编译器是否会让您编写没有返回值类型的函数定义;即您需要编码 int displayOne(int ...) 而不是 displayOne(int ...)

我会在继续之前解决这些问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-16
    • 2021-06-07
    • 2017-02-14
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    相关资源
    最近更新 更多