Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Aragorn992

Pointer/List trouble

This topic is 5921 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
you might try to put a tail on it so it is more organized and really cuts down on the complexity

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!