确保删除链表中的所有元素

时间:2019-02-24 23:57:50

标签: c++ list memory dynamic

我在使用称为element的基本结构的链表的代码中遇到了一个小问题。

程序本身是非常基本的:输入一个文本文件,输出每个不同的单词以及该单词出现的次数。

我的输出完全按照我的要求工作。

但是我在确保程序末尾删除所有分配的内存时遇到了麻烦。

Valgrind告诉我1个48字节的块仍在占用,问题与第39行有关:**temp = new element;**,但我仍然不知道我是否无法删除第一个或最后一个元素或我应该在哪里做。

有见识吗?

    std::string fileName;
    element * head = nullptr;
    element * temp = nullptr;

    // Input fileName to be read into file stream
    std::cout << "Input a file name to count the frequency of each word used: ";
    std::cin >> fileName;
    std::ifstream infile(fileName);
    if (!infile)
    {
            std::cout << "Error: File name [" << fileName << "] not found!" << std::endl;
            return 0;
    }

    // File stream inputs  text file into array, then takes it out if the word is already stored
    while(!infile.eof())
    {
            element * curr = nullptr;
            temp = new element;
            temp->next = nullptr;
            infile >> temp->word;

            if(head == nullptr){            // If list is empty, head becomes the first node
                    head = temp;
                    head->count++;
            } else {                        // but if the list isn't empty
                    curr = head;            // the current element goes inti the list
                    while (curr->next != nullptr){
                            if(curr->word == temp->word){   // If a duplicate it detected
                                    curr->count++;          // The count increases
                                    delete temp;
                                    temp = nullptr;         // and temp becomes null
                                    break;
                            }else{
                                    curr = curr->next;
                                    if(curr->word == temp->word){   // make sure to check the last element
                                            curr->count++;          // increase the count
                                            delete temp;
                                            temp = nullptr;         // and temp becomes null
                                            break;
                                    }
                            }
                    }
                    if(temp != nullptr && temp->word != ""){        // if none of those conditions are met
                    curr->next = temp;                              // then the new element gets put into the list
                    curr->next->count++;                            // and the count automatically goes to 1
                    }
            }
    }

    // Display the list using the current element istead of a for loop
    temp = head;
    while(temp != nullptr) {
            std::cout << "Word:     [ " <<  temp->word << " ] appears " << temp->count  << " time(s)." << std::endl;
            temp = temp->next;
    }

    // Delete memory allocated to the list
    while(head != nullptr) {
            temp = head->next;
            delete head;
            head = temp;
    }
    return 0;

}

0 个答案:

没有答案