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