Pointer/List trouble

Started by
8 comments, last by Aragorn992 21 years, 10 months ago
Im dealing with an ordered list here. What I need help in is removing a node in the list that is not at either end. The pointer, *head, contains the entire list of nodes (each node is a pointer to a struct which contains the actual value and a pointer to the next node). Now if I want to remove the first or last item thats easy:
*(head + pos) = *((head + pos)->next);  
NB: "pos" is the position in the list (i.e. pos = 0 is the first node). But I get an error (i.e. a pop up window/memory error) when it attempts to perform that piece of code on a node that is not at the end of the list. I presume its a memory problem or something related to that? Ive tried this:
(head + pos) = ((head + pos)->next);  
And get the compile error: "non-lvalue in assignment". Frankly im stumped and have spent at least 5 hours trying to figure this out. Any help would be appreciated. [edited by - Aragorn992 on June 1, 2002 6:01:05 AM]
Advertisement
Not sure how your list works internally, but I can explain the lvalue thing a bit.

Assignments can be split into lvalues and rvalues. They go on the left of the equals sign if they''re an lvalue, and the right if they''re an rvalue.

What the compiler is saying is that "head + pos" isn''t something you can assign a value to.

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
Ok thanks at least I know what that means, now I just need to figure out the top piece of code.
A good idea might be to use the Visual C debugger to step through your code.

Put a breakpoint (F9) on the first place you use your list, then step into your functions to see if there are any weird things going on with pointers and such.

I would recommend using an STL container, but these sort of situations are great for learning about algorithms and pointers.

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
If I recall correctly *(Head+Pos) increments the pointer head by Pos values, If your nodes happen to be stored one after the other fine, but if you are allocating nodes dynamicly this cannot be taken for granted, the memory may be fragmented.

if (head!=NULL) //check for empty{    if (head->Value==Value) //is value at start ?    {        if (head->next!=NULL) //more than one node        {            temp=head->next; //remember the next node            delete head; // delete data            head = temp; //assign next node to head        } else         {            delete head;            head=NULL; //no nodes left        }    } else    {        currentnode=head->next; //prep        oldnode=head;        while (currentnode->next!=NULL) and (currentnode->Value!=Value)        {            oldnode=currentnode;            currentnode = currentnode->next; //move onto next        }                  if (currentnode->Value=Value) //we found it        {            if (currentnode->next!=NULL) // in middle            {                temp=currentnode->next;                 delete currentnode;                oldnode->next = temp;            } else             {                 delete currentnode; // at end                oldnode->next=NULL; //sort out pointers            }        }    } //end else} // end if        



I may have made a few mistakes, but the principle is sound.


Dynamic memory and pointers:
http://pw1.netcom.com/~tjensen/ptr/pointers.htm

,Jay


Edit: I new I'd made a mistake (one at least).

[edited by - Jason Zelos on June 1, 2002 10:46:55 AM]
JUst guessing here but try:

*(head + pos) = *(*(head + pos)->next);

I am assuming next is a pointer...
when posting Code use:
N [ / s o u r c e ]
I prefer the tags, as I don''t like the white edit boxes. <br><br><small><hr>Helpful links:<br><a href="http://www.tuxedo.org/~esr/faqs/smart-questions.html">How To Ask Questions The Smart Way</a> | <a href="http://www.google.com/">Google</a> can help with your question | Search <a href="http://msdn.microsoft.com/">MSDN</a> for help with standard C or Windows functions<br></small>
Thanks for the replies, ill have a look at it tonight
you might try to put a tail on it so it is more organized and really cuts down on the complexity

This topic is closed to new replies.

Advertisement