Hi,
I am implementing the linked list delete as:

before delete: (delete a)
->(0x01:a)->(0x02:b)->(0x03:c)->

after delete:
->(0x01:b)->(0x03:c)->

ie instead of deleting a the content of next node is copied to current node and the next node is deleted. The code is as follows:

template<class type>void list_delete(struct _linked_list<type>** llist, type value)
{
	struct _linked_list<type>** node = list_search(llist, value);
	printf("search before delete: %p\n",*node);
	if (*node == NULL)
	{
		return;
	}

	struct _linked_list<type>** temp = &((*node)->next);
	printf("node->next: %p\n",(*node)->next);
	if ((*node)->next != NULL)
	{
		(*node)->value = (*node)->next->value;
		printf("before delete temp: %p %d\n",(*node)->next->next, (*node)->next->next->value);
		(*node)->next = (*node)->next->next;
		printf("before delete temp: %p %d\n",(*node)->next, (*node)->next->value);
	}
	else
	{
		*node = NULL;
	}

	free(*temp);
	printf("after delete temp: %p %d\n",(*node)->next, (*node)->next->value);
}

But the output I am getting is as follows:

addr: 0x2334010 value: 7
addr: 0x23341b0 value: 106
addr: 0x23340b0 value: 125
addr: 0x2334030 value: 159
addr: 0x23341f0 value: 191
addr: 0x2334050 value: 214
addr: 0x2334230 value: 294
addr: 0x2334170 value: 329
addr: 0x23340d0 value: 355
addr: 0x2334090 value: 463
addr: 0x2334250 value: 538
addr: 0x2334210 value: 542
addr: 0x2334110 value: 542
addr: 0x2334130 value: 636
addr: 0x2334190 value: 706
addr: 0x2334070 value: 730
addr: 0x2334270 value: 800
addr: 0x23340f0 value: 845
addr: 0x2334150 value: 896
addr: 0x23341d0 value: 983
7
search before delete: 0x2334010
node->next: 0x23341b0
before delete temp: 0x23340b0 125
before delete temp: 0x23340b0 125
after delete temp: 0x23340b0 0
addr: 0x2334010 value: 106
addr: 0x23340b0 value: 0
addr: 0x2334030 value: 159
addr: 0x23341f0 value: 191
addr: 0x2334050 value: 214
addr: 0x2334230 value: 294
addr: 0x2334170 value: 329
addr: 0x23340d0 value: 355
addr: 0x2334090 value: 463
addr: 0x2334250 value: 538
addr: 0x2334210 value: 542
addr: 0x2334110 value: 542
addr: 0x2334130 value: 636
addr: 0x2334190 value: 706
addr: 0x2334070 value: 730
addr: 0x2334270 value: 800
addr: 0x23340f0 value: 845
addr: 0x2334150 value: 896
addr: 0x23341d0 value: 983

ie as soon is the next node is deleted the next->next node's content become zero. I guess I need to delete the next pointer otherwise it would become a dangling pointer and cause memory leak. I am not sure why this happening and how to circumvent this.

Some help would be highly appreciated.

Thanks.

> ie instead of deleting a the content of next node is copied to current node and the next node is deleted.
Why overly complicate things?

Or more specifically, what happens when the next node is NULL, what do you copy then?
Or delete for that matter.

> ie instead of deleting a the content of next node is copied to current node and the next node is deleted.
Why overly complicate things?

Or more specifically, what happens when the next node is NULL, what do you copy then?
Or delete for that matter.

The node being deleted doesn't have a pointer to back reference to the previous node. So have to circumvent in this way. It can just forward reference to the next element so then all is left is to copy the content of the following element and delete it.

It appears like when next is deleted which is of type linked_list

template<class type> struct _linked_list
{
    type value;
    struct _linked_list<type>* next;
};

it also deletes its next. Otherwise there is no explanation I can conceive of.

Edited 3 Years Ago by Nick Evan: Fixed formatting

I figured out the problem. The temp is a double pointer so when node->next is changed to point to different value temp is still holding the address of the pointer.

This article has been dead for over six months. Start a new discussion instead.