sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
mathon@gmx.at's Avatar

Delete a node from a linkedlist


Question posted by: mathon@gmx.at (Guest) on October 21st, 2006 10:05 PM
hello,

i thought i create a new topic for that because i deal with a certain
problem. I have implemented all methods for a LinkdeList (for my
sequence class) and tested all correct. Now i only have problems with
the remove method to delete a certain node from the list. my remove
method looks like this:

Expand|Select|Wrap|Line Numbers
  1. void sequence::remove_current( )
  2. {
  3. if(!(is_item()))
  4. return;
  5.  
  6. node *target_ptr;
  7.  
  8. if(cursor != tail_ptr && many_nodes 1)
  9. {
  10. cout << "Cursor != tail_ptr" << endl;
  11. target_ptr = cursor;
  12. cursor = cursor->link();
  13. precursor->set_link(cursor);
  14. delete target_ptr;
  15. node* cursor1;
  16. node* precursor1;
  17. for(cursor1 = head_ptr; cursor1 != NULL; cursor1 = cursor1->link())
  18. {
  19. precursor1 = cursor1;
  20. }
  21. tail_ptr = precursor1;
  22. --many_nodes;
  23. return;
  24. }
  25.  
  26. else if(cursor == tail_ptr)
  27. {
  28. delete cursor;
  29.  
  30. --many_nodes;
  31. return;
  32. }
  33. else if (many_nodes == 1)
  34. {
  35. list_head_remove(head_ptr);
  36. start();
  37. --many_nodes;
  38. return;
  39. }
  40. }


So I used three cases - when the node should be deleted from the middle
of the list, when the node which should be deleted is at the end of the
list and when there is only one more node in the list. Unfortunately
the second case causes an error where the last node of the list should
be deleted:

The error occurs at the following statement:
delete cursor;

A popup window is displayed with the following error message:
Fehler:
Debug Assertion Failed!
Program: ...
File: dbgdel.cpp
Line: 52

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

For information on how your program can cause an assertion failure, see
the Visual C++ documentation on asserts.

Why can't I use the delete cursor statement to delete the last node?
(the cursor points to the last node) - has anybody an idea how to
define that correctly. The specification says when the last node is
deleted the cursor pointer should be then null again, thats why i tried
it with delete cursor.

matti

PS: The cursor pointer points to the actual node of the list, the
precursor to the previous node and the tail_ptr points every time to
the last node.

2 Answers Posted
David Harmon's Avatar
Guest - n/a Posts
#2: Re: Delete a node from a linkedlist

On 21 Oct 2006 14:25:40 -0700 in comp.lang.c++, Join Bytes! wrote,
Quote:
Originally Posted by
> else if(cursor == tail_ptr)
> {
> delete cursor;
>
> --many_nodes;
> return;
> }


Then tail_ptr and cursor both contain an invalid value, the former
pointer to the deleted node. The job here is half done.
Quote:
Originally Posted by
>Why can't I use the delete cursor statement to delete the last node?
>(the cursor points to the last node) - has anybody an idea how to
>define that correctly. The specification says when the last node is
>deleted the cursor pointer should be then null again, thats why i tried
>it with delete cursor.


I think it's because your code still has a few bugs in it and
deleting nodes leaves the list messed up. Write a simple function
to dump the list content, and call it after every operation... or at
lease after every delete while that is the part you are debugging.

Simplify!

By the way, "delete cursor" does not leave cursor as NULL, but
instead the old value is now invalid. Worse, tail_ptr has the same
value and is also invalid. "Invalid" here means you cannot
legitimately even compare it with another value.

In the simple case you write
Quote:
Originally Posted by
> cursor = cursor->link();


You should let the same line do that part also when deleting the
last node; not some special case. cursor->link() is NULL, the
assignment says in that case the result should be NULL, it all
works. I DO NOT mean to copy the line; I mean to fix the program
logic.

The loop,
Quote:
Originally Posted by
> for(cursor1 = head_ptr; cursor1 != NULL; cursor1 = cursor1->link())


is unnecessary and should be eliminated. The thing needed there is
something along the lines of
if(cursor == NULL)
tail_ptr = precursor;

Have you noticed yet that the only purpose of tail_ptr is to make
your task more difficult?

gw7rib@aol.com's Avatar
Guest - n/a Posts
#3: Re: Delete a node from a linkedlist


Join Bytes! wrote:
Quote:
Originally Posted by
hello,
>
i thought i create a new topic for that because i deal with a certain
problem. I have implemented all methods for a LinkdeList (for my
sequence class) and tested all correct. Now i only have problems with
the remove method to delete a certain node from the list.


I don't know what people have against double linked lists - ie each
element of the list contains a pointer to the element before as well as
the element after. Then deleting a node can be done as:

if (N == first) first = N -next; else N -prior -next = N -next;
if (N == last) last = N -prior; else N -next -prior = N -prior;
delete N;

with no need for precursors, counts of elements, or anything like that.
Paul.

 
Not the answer you were looking for? Post your question . . .
197,010 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,010 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors