首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >散列表:船舶记录

散列表:船舶记录
EN

Stack Overflow用户
提问于 2019-11-21 19:30:10
回答 1查看 120关注 0票数 0

我目前正在编写一个程序,它使用哈希表搜索船舶记录。它基于一个名为"shipRecords.txt“的文本文件,它包含以下内容:

代码语言:javascript
复制
  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()-删除给定序列号的记录。

我以前从未使用过哈希表,但如果有人能帮助我实现预期的输出,或者给我一些提示,我会非常感激的!

测试代码正在进行中.

代码语言:javascript
复制
#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;
}

电流输出

代码语言:javascript
复制
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

预期输出

代码语言:javascript
复制
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)!
EN

回答 1

Stack Overflow用户

发布于 2019-11-21 19:56:39

有几件事不起作用:

由于hashtable变量是由nullptr初始化的,因此nullptr

  • Function

  • 函数displayAll不会执行任何操作,而当hashptr不是

  • 函数时,其余变量应该正常工作,displayOne将始终打印“记录不存在”,因为没有其他退出该函数的方法。如果您的displayOne(int ...)

小于此大小(常量为10),并且所传递的参数大于1000,则不会有任何其他输出。

  • I不确定C++编译器是否允许您在没有返回值类型的情况下对函数定义进行编码;也就是说,您将需要编写int displayOne(int ...)代码而不是

在进一步研究之前,我会先解决这些问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58982464

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档