【问题标题】:C++ Linked List -- Reading in a file and sorting it based on numberC++ 链表——读入文件并根据数字对其进行排序
【发布时间】:2018-11-01 02:17:01
【问题描述】:

我正在阅读一个文件。它逐行填充如下内容:

e 2
b 1
a 3   
h 5
c 4

如果我按数字对这些进行排序,我会得到“海滩”这个词。我需要这样做,但规模更大。我目前有两个错误。我读到的有些行有一个空格和一个数字 [ " " 6 ],我的程序不会将它们附加/排序到我的链表中,因此这会导致分段错误,因为我的 for 打印循环是为了循环通过所有 53 个单词。

对于为什么空格没有出现在我的链接列表中,是否有任何解决方法?此外,大约 44/53 的单词使用我的 addInOrder 函数正确找到了它们的位置,但还有一些没有,这是有原因的吗?

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <ctype.h>

using namespace std;

struct ListNode
{
    string letter;
    string num;
    ListNode *next;
};

void append(ListNode *&h, string l, string n);
void addInOrder(ListNode *&h, string l, string n);
void printList(ListNode *h, int &lengthOfFile);
void deleteNode(ListNode *&h, string l, string n);
void deleteList(ListNode *&h);

int main()
{
    string letter;
    string num;
    string lines;
    int lengthOfFile = 0;
    const string FILENAME = "file link";
    ifstream inFile(FILENAME);

    /*if (inFile)
    {
        while (getline( inFile, lines )) 
        {
            lengthOfFile++;
            cout << "Hello...1" << endl;
        }
    }*/
    ListNode *head = nullptr;

    if (inFile)
    {
        string line;
        for (int lineNum = 1; getline(inFile, line); lineNum++)
        {
            stringstream ss(line);
            string word;

            for (int wordNum = 1; ss >> word; wordNum++)
            {

                if (wordNum == 1)
                {
                    char c = word[0];

                    if (isalpha(c))
                    {
                        cout << "letter: " << c << endl;
                        letter = c;
                    }
                    else if (word == "!" or word == ".")
                    {
                        cout << "letter: " << word << endl;
                        letter = word;
                    }
                    else if (word != "!" or word != ".")
                    {
                        cout << "letter: " << "  " << endl;
                        cout << "number: " << word << endl;
                        letter = "  ";
                        num = word;
                        lengthOfFile++;
                    }
                }
                if (wordNum == 2)
                {
                    cout << "number: " << word;
                    num = word;
                    lengthOfFile++;
                }
                if (wordNum == 2)
                {
                    cout << endl;
                    append(head, letter, num);
                    //cout << "letter: " << letter << "   and   num: " << num << endl;
                    cout << endl;
                    addInOrder(head, letter, num);
                    //cout << "letter: " << letter << "   and   num: " << num << endl;
                    cout << endl;
                    cout << endl;
                    cout << endl;
                }
            }
            cout << endl;
        }
    inFile.close();
    }

    cout << "lengthOfFile++;: " << lengthOfFile << endl;

    printList(head, lengthOfFile);
}

void append(ListNode *&h, string l, string n)
{
    // create a new ListNode and set data and next
    ListNode *newNode;
    newNode = new ListNode;
    newNode->letter = l;
    //cout << "l: " << l << endl;
    newNode->num = n;
    //cout << "n: " << n << endl;
    newNode->next = nullptr;
}

void addInOrder(ListNode *&h, string l, string n)
{
    // create a new ListNode and set data
    ListNode *newNode;
    newNode = new ListNode;
    newNode->letter = l;
    //cout << "l: " << l << endl;
    newNode->num = n;
    //cout << "n: " << n << endl;
    newNode->next = nullptr;

    // if list is empty, assign head to new ListNode; otherwise, 
    // find where to add in (non-descending) order
    if (h == nullptr)
    {
        h = newNode;
        newNode->next = nullptr;
    }
    else
    {
        ListNode *prev = nullptr;
        ListNode *curr = h;

        // find location to add
        while (curr != nullptr && curr->num < n)
        {
            prev = curr;
            curr = curr->next;
        }

        // if prev is nullptr, then we're adding to the beginning
        // of the list; else, adding to end or between two nodes
        if (prev == nullptr)
        {
            h = newNode;                    
            newNode->next = curr;
        }
        else
        {
            prev->next = newNode;
            newNode->next = curr;
        }
    }
}

void printList(ListNode *h, int &lengthOfFile)
{
    ListNode * ptr = h; 

    // loop through and print data
    for(int i = 0; i < lengthOfFile; i++)
    {
        cout << ptr->letter << " ";
        cout << ptr->num << " ";
        cout << endl;
        ptr = ptr->next;
    }
}

【问题讨论】:

  • 请熟悉MCVE 概念并相应地编辑您的问题。我相信并不是所有这些代码都需要代表这个问题——尤其是注释掉的文本
  • 你能详细说明你想表达的意思吗?我读到的有些行有一个空格和一个数字 [ " " 6 ],我的程序不会将它们附加/排序到我的链表中,因此这会导致分段错误,因为我的 for 打印循环是为了循环通过所有 53 个单词
  • 我的意思是,忽略代码中似乎存在的内存泄漏......绝对没有理由,其中有很多与问题无关的多余代码。如果您可以将代码格式化为仅重新创建问题所需的格式,那就太好了:)

标签: c++ list file linked-list segmentation-fault


【解决方案1】:

1) 问题出在下面一行:

ss >> word

operator &gt;&gt; 正在占用空白并寻找有效(非空白)字符。

要从流中获取单个字符,应使用ss.get()

2) 如果要与字符文字进行比较,请使用单撇号 '。 但是还要记住从line中提取一个字符。

【讨论】:

    猜你喜欢
    • 2022-01-11
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 2013-01-23
    • 1970-01-01
    • 2016-02-27
    相关资源
    最近更新 更多