473,219 Members | 1,576 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,219 software developers and data experts.

Delete a node from a linkedlist

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. }
  41.  
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.

Oct 21 '06 #1
2 2259
On 21 Oct 2006 14:25:40 -0700 in comp.lang.c++, ma****@gmx.at wrote,
> 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.
>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
> 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,
> 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?

Oct 22 '06 #2

ma****@gmx.at wrote:
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.

Oct 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Martin Magnusson | last post by:
I'm getting segmentation faults when trying to fix a memory leak in my program. The problem is related to lists of pointers which get passed around between objects. Here is a description of how...
3
by: MJ | last post by:
Hi I have created a prog for link list I am addig a node, displaying the LL contents, reversing the LL and deleting the LL In the delete() if I use free I am getting memory exception I have used...
2
by: Justin Crites | last post by:
I have an object which I want to be serializable. I have marked with with . The object only has a single data member, which is a LinkedList<int>. This linked list is a private member and cannot...
2
by: Bob Tinsman | last post by:
This problem shows up in Firefox 1.5.0.1 and Rhino 1.6R2. I've found that if I have an XML node expression that ends in a filter, I can't use it with the delete operator. In the following...
1
by: lg2530 | last post by:
template<class T> struct NODE { T data; NODE<T> * next; }; template<typename T> NODE<T>* QuickSortList( NODE<T>* list ) {}
4
by: paeez | last post by:
how can i add a node after ith node in a doubly linkedlist?(for example after the third node)
10
by: ac.c.2k7 | last post by:
Hello Everyone, The solution to this is to copy the data from the next node into this node and delete the next node!. 1. But if the node to be deleted is the last node. Then what should we do ?...
6
by: Phillip.Ross.Taylor | last post by:
When I designed my application I created an object called "Orderable" which exposes a public property "sequence". Then a few objects inherit from this. I'll just call them ObjectX for the sake...
6
by: APEJMAN | last post by:
I want to write several functions so that the program below would work. I am trying to find the functions for the main routine provided below and reveal a secret message. I dont wanne make any...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.