I want my program to retain its information within its linked list even after its ended. One way to do this is to read from a file and when done have the destructor save to that same file. I have the code to print to the file within the destructor, however its the reading from my file and storing that has me going nuts. Ive tried numerous ideas but i cant get it to work. Any one have any suggestions on what to add or fix? Beginner programmer here. I have seperate add, remove and clear functions applied.
The file im reading from looks like this:
HAHA
HEHE
MEME
OEL
URNE
WUWJ
I want to get each string into my linked list. Here is my code:
#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);
};
The functions:
#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;
}