删除某些节点

时间:2016-11-10 04:31:17

标签: c++

我创建了一个数据包结构。我读取文件的文本并将每个单词插入节点,然后如果有相同的字符串则递增计数。但我的问题是,我只想输出相同字符串的一个字符串及其使用次数。但每当我使用我的删除功能时,它会删除我文件中的所有内容,如果我不使用它,我会得到如下所示的输出。我不知道我做错了什么,有没有办法输出重复的字符串?

ofstream output;
struct  BagNode
{
    string dataValue;
    string dataCopy;
    int dataCountCopy;
    int dataCount;
    BagNode * next;
};
class Bag{
private:

BagNode * head;

public:

Bag()
{
    head = NULL;

}
void insert(string v)
{
    if(head == NULL){ //empty list
        head = new BagNode;
        removePunct(v);
        head->dataValue = v;
        transform(v.begin(), v.end(), v.begin(), ::tolower);
        head->dataCopy = v;
        head->next = NULL;

    }
    else
    {
            BagNode * n = new BagNode;      // new node
            removePunct(v);
            n->dataValue = v;
            transform(v.begin(), v.end(), v.begin(), ::tolower);
            n->dataCopy = v;
            BagNode * current = head;           //for traversal
            //current = head;
            n->dataCount = 0;
                if(current->dataCopy > v)
                {                       
                    n->next = head;
                    head = n;
                }
                else{           //mid and tail insert
                    while(current->next && current->next->dataCopy < v)
                    {
                        current = current->next;
                    }
                    n->next = current->next;
                    current->next = n;

                }   

    }
    BagNode * check = new BagNode;
    for(check = head; check->next != NULL; check = check->next) 
    {
        if(check->dataCopy == v)//isSame(check->dataValue, v)) 
        {
            check->dataCount++;
        }

    }

}
 bool remove(string v) //bool
{
    bool status;
    if(head == NULL){
        status = false;
    }
    else if(head->dataCopy > v)
    {//(head->dataValue > v){
        status = false;
    }
    else if(head->dataCopy == v)
    {
        BagNode * t = head;
        head = head->next;
        delete t;
        status = true;
    }
    else//general case
    {
        BagNode * current = head;
        while(current->next && current->next->dataCopy < v){
            current = current->next;
        }
        if(current->next == NULL)
        {
            status = false;
        }
        else if(current->next->dataCopy == v)   //found it
        {
            BagNode *t = current->next;
            current->next = current->next->next;
            delete t;
            status = true;
        }
        else
        {
            status = false;
        }
    }
    return status;
}
void traverse()
{
    BagNode * current;

    current = head;
    while(current)
    {
            output << current->dataValue << " (" << current->dataCount << ")" << " ";
            current = current->next;

    }
    cout << endl;
}
  

输出:10Annette(1)1805(1)7(1)a(1)a(2)a(3)a(4)a(5)a(6)全部(1)全部(2)一个(1)和(1)和(2)和(3)和(4)和(5)和(6)和(10)和(7)

if(!inputFile)
    {
        cout << "Could Not Open " << fileName << " File" << endl;
        exit(EXIT_FAILURE);
    }
    else
    {
        while(inputFile >> text)
        {
            theBag.insert(text);

        }
        cout << "Processing File Complete" << endl;
        cout << "Please Enter An Output File Name: ";
        getline(cin,outputFilename);
        output.open(outputFilename);
        theBag.traverse();
        theBag.remove(text);
        inputFile.close();
        output.close();
    }

1 个答案:

答案 0 :(得分:1)

如果你在insert函数中看到这里,你实际上是用这个值触及每个节点。因此,如果v = "And"每一个&#34;和&#34;这个词正在增加它的数据计数。这会使您在每个节点上获得正确的单词计数。

for(check = head; check->next != NULL; check = check->next) 
{
    if(check->dataCopy == v)//isSame(check->dataValue, v)) 
    {
        check->dataCount++;
    }
}

似乎你可以利用这种行为使你的插入更加简单。