Note that there are some explanatory texts on larger screens.

plurals
  1. POCursor in Linked List changing value for no apparent reason
    text
    copied!<p><em><strong>Edit</em></strong></p> <p><em><strong>For simplicity:</em></strong> I just want to make the most basic possible cursor that simply goes trough my list without changing in any way / shape or form my "begin". I still want begin to be changed when I return something though, just not before. So is it possible to make a pointer to begin, go trough the list without changing anything except at the very end when I want to add a new node ?</p> <p><em><strong>Also</em></strong> Can I just go trough the list with a simple pointer that is not a "Node" ?</p> <p><em><strong>/Edit</em></strong></p> <p>I have a simple (singly) linked list to make as part of my homework. Of-course I also have a lot to do besides that, but after I get the list out of the way everything should be strait forward, but while I have been using C++ for a while (it was borland C++) a lot of what I knew is either half-forgotten or outdated. I have programmed in python for a while but all that means is that I keep getting frustrated with how pointers work in C++.</p> <p>My problem is when I try to add a new Node to the list, my cursor behaves in an unusual manner, I will explain below:</p> <p><em><strong>EDIT:</em></strong> Ok, I changed the:</p> <pre><code>Node *cursor; cursor = new Node cursor = begin; </code></pre> <p>fiasco, but the result is the same, after the declaration cursor and begin both point at the same memory location (something like: 0x32ce8).</p> <p><em><strong>/EDIT</em></strong></p> <pre><code>Node *add_node (Node *begin,string type, int sum, int ap_nr) // begin is the first node in the list { // if first node is dummy node if (begin-&gt;ap_nr == -1) { begin-&gt;type = type; begin-&gt;ap_nr = ap_nr; begin-&gt;sum = sum; begin-&gt;next = 0; return begin; } // else create new node and insert it in sorted position else { // EDIT: Node *cursor = begin; // Same problem //if node should be inserted before first node (begin) if (ap_nr &lt;begin-&gt;ap_nr) { cursor-&gt;ap_nr = ap_nr; cursor-&gt;type = type; cursor-&gt;sum = sum; cursor-&gt;next = begin; return cursor; } </code></pre> <p>Always when I debug, begin has a similar form: 0x32ce02, when I create my "cursor" it has a vastly different form (longer also), but when I do this : cursor = begin, then cursor becomes something like this 0x32df02.</p> <p>However the problem is when I get to "if (ap_nr ap_nr)" then for absolutely no feasible reason cursor becomes: 0x32ce02 and "cursor -> next = begin" ensures an infinite loop. And no matter how many nodes I add this always happens so whenever I print the list it's an infinite stream of the last added Node.</p> <p>Am I doing something wrong ? is it the declaration or the alocation, creation ? something ?</p> <p>Also if I have a pointer *begin somewhere in another module, and with this function I return a new begin ... that should work, right ?</p> <p>P.S. I would also appreciate a simple counter solution (another way to do this if mine is just not good)</p> <p>Also I should point out how I made my list. It is just a simple linking of nodes:</p> <pre><code>struct Node { string type; int ap_nr; int sum; Node *next; }; </code></pre>
 

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