Using MK27's suggestion would make it much easier to navigate through the matrix, but since there will be gaps inside the matrix, I'm not sure how that will work logically.
Printable View
Using MK27's suggestion would make it much easier to navigate through the matrix, but since there will be gaps inside the matrix, I'm not sure how that will work logically.
i cant change the structs. they have only 2 pointers (down and right)
Is this mandated by an instructor? I'm not impressed with the guy(girl) if it is...
i cant change it.
the structs must have only this 2 pointers (right and down)
Since your first instruction is
this function will recurse throught the entire ->right vector before it does the first ->down vector -- which will not be the first one, it will be the last one in the first row! So you are freeing this in a clockwise spiral -- or rather it will complete all the rights in order, and then the downs from that first row in reverse order. Like snakes and ladders backwards, then forwards in alternating rows. Except of course it never gets beyond the first element of the second row because of the double free.Code:freeArray(head->right);
You may need some clever printf's in there to debug the recursion calls (if you want, and I'll recommend trying), but I'm pretty sure this is correct anyway. So you would be better off using two functions -- one which goes only right, and one which goes only down, and nest one inside the other.
:)
If you think hard enough you should be able to get this.
I would like to highlight post #7:
Quote:
Originally Posted by tabstop
I agree with myself -- there's no need to try to work two directions at once, and in fact there are many things wrong with it. Just walk through each row[i], freeing to the right until you get NULL. There's no reason to worry about down at all.
That shouldn't be necessary to have a down and a right function... in fact I don't think it would work... ... Please note I haven't compiled this...
Like I said I haven't compiled it. I might not be passing address correctly, I'm not really handy with pointers tbh... but I think the logics of the functions works...Code:Array * main_head; // I hate globals, but this is the only way I could think of to solve certain issues.
void set_nulls(int address, Array ** head) { //address will hold the address of the node we are removing
if (head->down == NULL) {
if (head->right == NULL) {
return;
} else {
if (head->right == *address) {
head->right = NULL;
} else {
set_nulls(address, head->right);
}
}
} else {
if (head->down == *address) {
head->down = NULL;
} else {
set_nulls(address, head->down);
}
}
}
void freeArray( Array ** head ) {
if (head->down == NULL) {
if (head->right == NULL) {
//Search through the array for any nodes that point to this node and set the pointer to null
set_nulls(&node, &main_head);
free(head->right);
free(head->down);
free(head);
} else {
freeArray(head->right);
}
} else {
freeArray(head->down)
}
}
You do appear to correct. I hadn't thought about it that way...
thnks a lot
i got it
no recusive function. the simple and effective way
again, thanx a lot guys
You just need two phases:
In practice, you'll want to iterate, not recurse, but it should demonstrate the idea.Code:void freeArray(Array * head)
{
if(!head) return;
freeArray(head->down);
freeToTheRight(head);
}
void freeToTheRight(Array *head)
{
if(!head) return;
freeToTheRight(head->right);
free(head);
}
Brewbuck! You Must Read Minds.
Why? What's wrong with recursion?Quote:
In practice, you'll want to iterate, not recurse