0

我希望我的程序即使在结束后也能将其信息保留在其链接列表中。一种方法是从文件中读取,完成后将析构函数保存到同一个文件中。我有代码可以打印到析构函数中的文件,但是它从我的文件中读取和存储让我发疯了。我尝试了很多想法,但我无法让它发挥作用。有人对添加或修复什么有任何建议吗?初级程序员在这里。我应用了单独的添加、删除和清除功能。

我从中读取的文件如下所示:

HAHA
HEHE
MEME
OEL
URNE
WUWJ

我想将每个字符串都放入我的链接列表中。这是我的代码:

#pragma once
#include <string>
using namespace std;

class StringList;

class StringList
{
    private:
    struct StringListNode
    {
        StringListNode *pPrev;
        string          data;
        StringListNode *pNext;
    };
    public:
        StringList();
        ~StringList();
        void addToBottom(string s);
        void addToTop(string s);
        void remove(string s);
        void add(string s);
        string print();
        void clear();
        bool isEmpty() {return (pTop==NULL);}
    private:
        StringListNode * pTop;
        StringListNode * pBottom;
        StringListNode *find(const string &s);
};

功能:

#include "StringList.h"
#include <string>
#include<iostream>
#include<fstream>

using namespace std;

StringList::StringList() 
{
   std::ifstream input( "Read.txt" );
   pTop = NULL;
   pBottom = NULL;
   for( std::string line; getline( input, line ); )
   {
      add(line);
   }
}


StringList::~StringList() {
    ofstream ofs("Read.txt");
    StringListNode *next;
    for (StringListNode *sp = pTop; sp != 0; sp = next)
    {
        ofs << sp->data << endl;
        next = sp->pNext;
        delete sp;
    }
}
void StringList::add(string s) //adds and places in alphabetical order
{
    if(pTop)
    {
        StringListNode *iter = pTop;
        while (iter && s > iter->data)
            iter = iter->pNext;

        if (iter)
        {
            // insert before
            StringListNode *A=new StringListNode;
            A->data = s;
            A->pNext = iter;
            A->pPrev = iter->pPrev;
            if (iter->pPrev)
                iter->pPrev->pNext = A;
            else
                pTop = A;
            iter->pPrev = A;
            return;
        }
    }

    // add to bottom
    addToBottom(s);
}

StringList::StringListNode *StringList::find(const string &s) //basic search function
{
    StringListNode *sp = pTop;   // Search
    while (sp != 0 && sp->data != s)
        sp = sp->pNext;
    return sp;
}


void StringList::addToTop(string s) //add to top of nodes
{
    if(isEmpty())
    {
        StringListNode * pNewNode;
        pNewNode = new StringListNode;
        (*pNewNode).data = s;
        pTop=pNewNode;
        pBottom=pNewNode;
        (*pNewNode).pPrev = NULL;
        (*pNewNode).pNext = NULL;
    }
    else //it's not empty
    {
        StringListNode * pNewNode;
        pNewNode = new StringListNode;
        (*pNewNode).data = s;
        (*pNewNode).pNext = pTop;
        (*pTop).pPrev = pNewNode;
        (*pNewNode).pPrev =NULL;
        pTop=pNewNode;
    }
}

void StringList::addToBottom(string s) // add to bottom
{
    if(isEmpty())
    {
        StringListNode * pNewNode;
        pNewNode = new StringListNode;
        (*pNewNode).data = s;
        pTop=pNewNode;
        pBottom=pNewNode;
        (*pNewNode).pPrev = NULL;
        (*pNewNode).pNext = NULL;
    }
    else
    {
        StringListNode * pNewNode;
        pNewNode = new StringListNode;
        (*pNewNode).data = s;
        (*pBottom).pNext = pNewNode;
        (*pNewNode).pPrev =  pBottom;
        (*pNewNode).pNext =NULL;
        pBottom=pNewNode;
    }
}

string StringList::print()
{
    string result;
    StringListNode * pCurrent;
    pCurrent=pTop;
    while(pCurrent!=NULL)
    {
        result+=(*pCurrent).data+"\n";
        pCurrent=(*pCurrent).pNext;
    }
    return result;
}

void StringList::clear()
{
    pTop = NULL;
    pBottom = NULL;
}

void StringList::remove(string s)
{
    StringListNode *curr = this->find(s);
    if (curr->pPrev != 0)
        curr->pPrev->pNext = curr->pNext;
    if (curr->pNext != 0)
        curr->pNext->pPrev = curr->pPrev;
    if (pTop == curr)
        pTop = curr->pNext;
    if (pBottom == curr)
        pBottom = curr->pPrev;
}
4

2 回答 2

1

您的构造函数中有错误,特别是内存泄漏:

for each string s  // read from file but how do i write this?
{
    cur = new StringListNode(s);  // call the constructor of StringListNode
    cur = cur->pNext;
}

您创建了一个节点,但您立即将指向该节点的指针替换为该节点的初始next指针。

至于从文件中读取值,我建议您在 Stack Overflow 中搜索“[c++] read from file”。最近这种情况太多了。

编辑 1:构造函数更正

while (read data succeeds)
{
   add(data); // why not use your own function?
}

为了使追加更有效,您可能需要维护指向最后一个节点的指针。

void append(data)
{
   p = new node(data);
   last->next = p;
   last = p;
}
于 2013-07-16T20:40:25.277 回答
0

我确实在您的删除功能中注意到您忘记了 if 语句。如果您输入的内容不在列表中,会发生什么?

我相信你需要类似的东西

if (curr == 0)
{
    return;
}

也尝试为您的文件输入/输出

ifstream in;
in.open("Read.txt");

StringListNode * pCurrent;
pCurrent = new StringListNode;
pCurrent = pTop;    

while(pCurrent && (in >> pCurrent->data)) 
{
   pCurrent = pCurrent->pNext;
}    
in.close();


StringList::~StringList()
{
ofstream out;
out.open("Read.txt");

StringListNode * pCurrent;
pCurrent = new StringListNode;
pCurrent = pTop;
while(pCurrent != 0)  
{
    out << pCurrent->data << endl;
    pCurrent = pCurrent->pNext;
}
out.close();
}

该输出应该有效。我刚刚验证了这一点。我认为这应该可以解决您的输入崩溃问题。

于 2013-07-17T02:43:10.340 回答