Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem either adding to or traversing a linked list
    text
    copied!<p>I have a problem either adding to or traversing a linked list. The main item class is used by another class but I can add the correct number of these but it looks like when I add more data to the list the app no longer works.</p> <p>I am not sure exactly where the error is. I know that when I try to traverse the list the application crashes. Any ideas or any improvements would be appreciated.</p> <p>I can make the crash not happen by changing the AddOccurence method to not do the while loop.</p> <p>Do </p> <pre><code>void Item::AddOccurence(int Item,int placeInLine){ ItemOccurence* ocr=myHead; if(ocr) { } </code></pre> <p>instead of</p> <pre><code>void Item::AddOccurence(int Item,int placeInLine){ ItemOccurence* ocr=myHead; while(ocr) { } </code></pre> <p>Basically hitting the first node but no more.</p> <p>I have an object that contains a list. Here is the .h file #include using namespace std;</p> <pre><code>class ItemOccurence{ public: ItemOccurence(int line,int placeInLine,ItemOccurence* link=NULL) :myLine(line),myPlaceInLine(placeInLine),myLink(link){} int myLine; int myPlaceInLine; ItemOccurence* myLink; }; class Item { public: Item(); Item(string Item,int line,int placeInLine); virtual ~Item(); void deallocate(ItemOccurence* p); void AddOccurence(int Item,int placeInLine); string myItem; ItemOccurence* myHead; private: bool isEmpty(); }; </code></pre> <p>And the .cpp file</p> <pre><code>#include "Item.h" #include &lt;string&gt; #include&lt;iostream&gt; using namespace std; Item::Item(string Item,int line,int placeInLine):myHead(NULL){ myItem=Item; myHead= new ItemOccurence(line,placeInLine,NULL); } Item::Item():myHead(NULL){ myHead=0; } Item::~Item() { deallocate(myHead); myHead=0; } void Item::deallocate(ItemOccurence* p){ ItemOccurence* tmp; while(p){ tmp=p; p=p-&gt;myLink; delete tmp; } } void Item::AddOccurence(int Item,int placeInLine){ ItemOccurence* ocr=myHead; while(ocr) { cout&lt;&lt;"orrucence head while adding " &lt;&lt; myHead-&gt;myLine &lt;&lt; " " &lt;&lt; myHead-&gt;myPlaceInLine &lt;&lt;"\n"; ocr=ocr-&gt;myLink; } myHead = new ItemOccurence(Item,placeInLine,myHead); return; } bool Item::isEmpty(){ if(myHead) return false; else return true; } </code></pre> <p>EDIT: I updated AddOccurence to be.</p> <pre><code>void Item::AddOccurence(int line,int placeInLine){ ItemOccurence* prev = myHead; ItemOccurence* curr = myHead-&gt;myLink; while(curr){ prev=curr; curr=curr-&gt;myLink; } // insert new ItemOccurence cout&lt;&lt;"adding " &lt;&lt;line&lt;&lt; " and " &lt;&lt; placeInLine &lt;&lt;"\n"; prev-&gt;myLink = new ItemOccurence(line,placeInLine); return; } </code></pre> <p>But I am still crashing. I am trying to debug but not sure what to look for.</p>
 

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