Note that there are some explanatory texts on larger screens.

plurals
  1. POConstructor function reads from file and stores in linked list
    primarykey
    data
    text
    <p>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.</p> <p>The file im reading from looks like this:</p> <pre><code>HAHA HEHE MEME OEL URNE WUWJ </code></pre> <p>I want to get each string into my linked list. Here is my code:</p> <pre><code>#pragma once #include &lt;string&gt; 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 &amp;s); }; </code></pre> <p>The functions:</p> <pre><code>#include "StringList.h" #include &lt;string&gt; #include&lt;iostream&gt; #include&lt;fstream&gt; 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 &lt;&lt; sp-&gt;data &lt;&lt; endl; next = sp-&gt;pNext; delete sp; } } void StringList::add(string s) //adds and places in alphabetical order { if(pTop) { StringListNode *iter = pTop; while (iter &amp;&amp; s &gt; iter-&gt;data) iter = iter-&gt;pNext; if (iter) { // insert before StringListNode *A=new StringListNode; A-&gt;data = s; A-&gt;pNext = iter; A-&gt;pPrev = iter-&gt;pPrev; if (iter-&gt;pPrev) iter-&gt;pPrev-&gt;pNext = A; else pTop = A; iter-&gt;pPrev = A; return; } } // add to bottom addToBottom(s); } StringList::StringListNode *StringList::find(const string &amp;s) //basic search function { StringListNode *sp = pTop; // Search while (sp != 0 &amp;&amp; sp-&gt;data != s) sp = sp-&gt;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-&gt;find(s); if (curr-&gt;pPrev != 0) curr-&gt;pPrev-&gt;pNext = curr-&gt;pNext; if (curr-&gt;pNext != 0) curr-&gt;pNext-&gt;pPrev = curr-&gt;pPrev; if (pTop == curr) pTop = curr-&gt;pNext; if (pBottom == curr) pBottom = curr-&gt;pPrev; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload