EDIT: Problem: Deleting a node between 2 nodes, and then linking the outer nodes with each other.
After learning and building several quadtrees and octrees, and really liking subdivision, I decided to use subdivision in my server. But, only placing nodes within themselves. My problem is, I cant figure out how to link the previous node, to the next one if the node is in the middle. I am wondering if I am doing this correctly. Below is my code and I comment where the problem area is. Any help would be great!
bool deleteNode ( SOCKET s , client_information_class *prev ) {
bool return_value = false;
if ( node[0]->client_socket != s ) {
if ( node[0]->node != NULL ) {
return_value = node[0]->deleteNode ( s , *node );
} else {
cout << endl << "Can not call deleteNode anymore ... Did not find the node to delete ... " << endl;
return return_value;
}
} else {
if ( node[0]->client_state == 1 )
InterlockedDecrement(&connection_count);
if ( node[0]->node != NULL ) { // there is a next node
client_information_class **next = node[0]->node;
if ( next[0]->node != NULL ) {
// problem area
cout << endl << "next[0]->node is not null" << endl;
prev->node = next[0]->node[0]->node; // ??? I know its wrong
} else {
// problem area
cout << endl << "next[0]->node is null" << endl;
prev->node = next[0]->node; // ??? I know its wrong
}
delete node;
} else { // its the last one
prev->node = NULL;
delete node;
}
InterlockedDecrement(&socket_count);
return_value = true;
}
return return_value;
}
So if I got your problem right, you have a trouble inserting an item inside a linked list. I really really hope this is the issue, otherwise all the typing I’ve done was totally in vain.
I didn’t really get all of your code (since it’s a little out of context), but I’ll show you the logic on an example. I will use nodes that have the following structure:
So we will make a linked list in which each item has pointers to the previous and the next items. The logic for more complicated structures (like if you have, for example, left + right + child + parent pointers) is the same, the only difference is that you will have to set all of them. It doesn’t really change much.
So, to construct the list, we will need a couple of constructors that will handle different operations:
the default constructor to make an empty node:
a constructor that takes a pointer to the last item in the list, and appends itself to the end of the list:
And finally a constructor to insert in the middle (which, I believe, is what you want):
Now we have the full set of instruments to do whatever we want. The one last thing is deleting a node:
or, a more readable syntax:
let’s make a sample list of, say, 10 elements:
Here, all the elements in the list are properly linked, and have their i field containing 0 value. Let’s insert another element somewhere in the middle, for example, between the third and the fourth elements:
Now we can easily check if the insertion went correctly. Let’s type all the element’s data:
The result will be
0 0 0 9 0 0 0 0 0 0 0.Finally, let’s delete the just-inserted node:
If you will not output the values in the same vay, you will see only zeroes, which means the node was successfully deleted.
If you have more pointers in your nodes, all you have to do is to handle them properly in the same way. Another hint for you is to avoid constructions like
this->next->next->prev. It is hard to tell that you are referring to the “prev” pointer of the element that is third from the current one. Instead, use something like:or maybe
and then work with the pointer you got.