Edit
For simplicity: 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 ?
Also Can I just go trough the list with a simple pointer that is not a “Node” ?
/Edit
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++.
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:
EDIT: Ok, I changed the:
Node *cursor;
cursor = new Node
cursor = begin;
fiasco, but the result is the same, after the declaration cursor and begin both point at the same memory location (something like: 0x32ce8).
/EDIT
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->ap_nr == -1)
{
begin->type = type;
begin->ap_nr = ap_nr;
begin->sum = sum;
begin->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 <begin->ap_nr)
{
cursor->ap_nr = ap_nr;
cursor->type = type;
cursor->sum = sum;
cursor->next = begin;
return cursor;
}
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.
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.
Am I doing something wrong ? is it the declaration or the alocation, creation ? something ?
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.S. I would also appreciate a simple counter solution (another way to do this if mine is just not good)
Also I should point out how I made my list. It is just a simple linking of nodes:
struct Node {
string type;
int ap_nr;
int sum;
Node *next;
};
The pointers
cursorandbeginpoint to the same memory location because you explicitly say so:Node* cursor = begin;literally says “create a pointer variable namedcursorwhich points to the same location asbegin.” So it is no surprise that it does.Edit: Removed advice based on a wrong guess what the code is intended to do and changed it to more applicable advice
From the comment, I now understand that you want to insert a node at a position so that the field
ap_nris increasing in the resulting list, assuming it is increasing originally (if that still isn’t right, please state clearly what you do want).For that case, the initialization of
cursoris of course now correct. However it is not correct to modify the objectcursorpoints to: You want to insert a new node before that node. But for that you have to make several changes:First, you need another pointer variable, which holds a pointer to a newly created node, like
And then you have to insert that node into the list. That is, instead of
cursor->ap_nr=ap_nr;etc. you have to usenew_node->ap_nr=ap_nr;etc. Also, the node following it is of course not the first node of the list (pointed to byfirst) but the one you just found (pointed to bycurrent).However, now you have a problem: You have to insert that new node into the list, which means you have to modify the
nextpointer of the previous node (but to point not tobegin, but to the newly created node!). But you don’t have any more the pointer to the previous node, because your list is singly-linked, that is, you don’t have a pointer from the found element to the previous element. But to insert the element, you have to change thenext.However what you do have is a pointer to the next node. Therefore a better strategy is to have your
cursorpoint to the previous node, and then consistently usecursor->nextinstead ofcursor(except when movingcursor, of course). That way you can, after you’ve setnew_node->next, writecursor->next = new_node;Other things which are missing from your code are checks that
currentis not null (which it will be at the end of the list) and ther code acrtually moving yourcursorforward (which belongs into anelsepart of your innerif).Actually I now notice that your blocks are not closed, so the moving-forward code might be there in your actual code.
At the end, some general advice: You probably would have an easier time writing that code if you modularized the code of your function: Have one function to insert a new node after a given one (changing only the
nextpointers, and returning a pointer to the newly inserted code), have another function to find the node after which the new node should be inserted, and have your functionadd_nodeonly call those other functions. That way, in each function you can concentrate on one of the sub-problems.