C++链接列表节点计数(需要帮助)

问题描述:

我试图创建一个程序,从文本文件中获取字符串输入,逐个字将内容插入列表中。我也必须计算重复的数字。我的程序适用于小输入文本文件(1行字符串)。但是,只要我用更大的文本文件提供它,就会崩溃。任何帮助都会很棒。C++链接列表节点计数(需要帮助)

这里是我的代码:

#include <iostream> 
#include <fstream> 
#include <sstream> 

using namespace std; 

class Bag 
{ 
    private: 
     struct BagNode 
     { 
      string dataValue; 
      int dataCount; 
      BagNode *next; 
      BagNode(string); 
     }; 

     BagNode *root; 
     string removePunctuations(string); 
     string toLower(string); 
     void insertData(string); 

    public: 
     Bag(); 
     void procesFile(string, string); 
     void removeData(string); 
     void traverse(); 
}; 

Bag::BagNode::BagNode(string _data) 
{ 
    dataValue.assign(_data); 
    dataCount=1; 
    next = NULL; 
} 

Bag::Bag() 
{ 
    root = NULL; 
} 

void Bag::procesFile(string ifile, string ofile) 
{ 
    ifstream infile; 
    infile.open(ifile.c_str()); 
    if (!infile.good()) 
    { 
     cout<<"Input file not opening."<<endl; 
     return; 
    } 

    string line; 
    while(getline(infile,line)) 
    { 
     stringstream lineStream(line); 
     string token = ""; 
     while(lineStream >> token) 
     { 
      insertData(removePunctuations(token)); 
     } 
    } 
    infile.close(); 
    traverse(); 
    cout<< endl <<"File processed successfully." << endl; 
} 

string Bag::removePunctuations(string data) 
{ 
    int length = data.size(); 
    for(int i = 0; i < length; i++) 
    { 
     if(ispunct(data[i])) 
     { 
      data.erase(i--, 1); 
      length = data.size(); 
     } 
    } 
    return data; 
} 

string Bag::toLower(string data) 
{ 
    for(int i = 0; data[i]; i++){ 
     data[i] = tolower(data[i]); 
    } 
    return data; 
} 

void Bag::insertData(string data) 
{ 
    BagNode *n = new BagNode(data); 
    if (root == NULL) 
    { 
     root = n; 
     return; 
    } 

    BagNode *temp = root; 
    BagNode *prev = NULL; 

    string tdata; 
    data.assign(toLower(data)); 
    while(temp != NULL) 
    { 
     tdata.assign(temp->dataValue); 
     tdata.assign(toLower(tdata)); 
     if (tdata.compare(data) == 0) 
     { 
      temp->dataCount++; 
      return; 
     } 
     else 
     { 
      if (data.compare(tdata) < 0) 
      { 
       if (temp == root) 
       { 
        n->next = temp; 
        root = n; 
        return; 
       } 
       else 
       { 
        n->next = temp; 
        prev->next = n; 
        return; 
       } 
      } 
     } 
     prev = temp; 
     temp = temp->next; 
    } 

    n->next = temp; 
    prev->next = n; 
} 

void Bag::removeData(string data) 
{ 
    BagNode *temp = root; 
    BagNode *prev = NULL; 

    if (root->dataValue.compare(data)==0) 
    { 
     if (root->dataCount > 1) 
      root->dataCount--; 
     else 
     { 
      delete root; 
      root = NULL; 
     } 
     cout<<"Data removed successfully."<<endl; 
     return; 
    } 
    while (temp != NULL) 
    { 
     if (temp->dataValue.compare(data)==0) 
     { 
      if (temp->dataCount > 1) 
       temp->dataCount--; 
      else 
      { 
       prev->next = temp->next; 
       delete temp; 
       temp = NULL; 
      } 
      cout<<"Data removed successfully."<<endl; 
      return; 
     } 
     prev = temp; 
     temp = temp->next; 
    } 
    cout<<"Data not found match."<<endl; 
} 

void Bag::traverse() 
{ 
    if (root == NULL) 
    { 
     cout<<"No data."<<endl; 
     return; 
    } 

    BagNode *temp = root; 
    while(temp != NULL) 
    { 
     if (temp->dataCount > 1) 
      cout << temp -> dataValue << "(" << temp->dataCount << ")" << endl; 
     else 
      cout << temp -> dataValue << endl; 
     temp = temp->next; 
    } 
} 

int main(int argc, char *argv[]) 
{ 
    bool outputConsole = false; 
    string infile, outfile = "\0"; 
    cout << "Welcome!" << endl; 
    int option = -1; 
    do{ 
     if (argc==1 || option == 1) 
     { 
      cout << "Enter the input file: "; 
      cin >> infile; 
      cout << "Enter the output file: "; 
      cin >> outfile; 
     } 
     else 
     { 
      infile.assign(argv[1]); 
      if (argc == 3) 
       outfile.assign(argv[2]); 
     } 

     Bag b; 
     b.procesFile(infile,outfile); 
     //b.traverse(); 

     cout<<endl<<"If you want to input another file press 1 or 2 to quit: "; 
     cin>>option; 
    }while (option != 2); 

    return 0; 
} 
+0

回答您的问题的正确工具是您的调试器。看看[如何调试小程序](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Danh

+0

在崩溃期间你会收到任何错误信息吗?任何异常都会抛出? –

+0

我得到一个错误,说:调试断言失败! 第56行:表达式c> = -1 && c LucianoLe

如果单词的顺序是不是一个问题,你应该尝试并使用哈希表,而不是一个链表哈希表适用于跟踪重复的。这将导致O(1)插入操作(在理想情况下)

+0

我实际上需要按字母顺序输出单词。我得到那部分工作tho :) – LucianoLe