c++ - Trouble with a recursive algorithm and pointers -
so i'm trying make algorithm starts @ first "room" , recursively goes outward , starts deleting rooms outside in. room struct 4 "doors" (pointers rooms): north, south, east, , west.
the function takes 2 arguments: pointer current room , char (to identify direction: north, south, east, or west).
here logic algorithm (roomdelete):
base case
- start @ first room , call function (roomdelete) on non-null pointers; input function calls should appropriate pointer room n/s/e/w, , appropriate char representing direction n/s/e/w.
- check see pointers (n/s/e/w) null --> delete current room.
- done/return.
recursion
- make sure not backtrack (travel in direction came from), using char hold value opposite of direction char.
- call function on non-null, non-backtrack pointers/directions.
- break connection previous room.
- check see pointers null --> delete current room.
here simple picture on rooms/pointers like: http://i.imgur.com/btkz5jb.png
i have code tried test. if have room (by itself), function works. room thrown mix, function never returns/finishes. i'm not sure why. logic sound? missing something? appreciated.
code:
#include <iostream> #include <string> #include <cstdlib> #include <ctime> using namespace std; #define num_doors 4 struct room { struct room * north; struct room * south; struct room * east; struct room * west; } ; int roomdelete(room *, char); int main(void) { room * test_ptr = new room; cout << "created room @ location: " << test_ptr << endl; test_ptr->north = null; test_ptr->south = null; test_ptr->east = null; test_ptr->west = null; test_ptr->north = new room; cout << "created room @ location: " << test_ptr->north << endl; test_ptr->north->north = null; test_ptr->north->south = test_ptr; test_ptr->north->east = null; test_ptr->north->west = null; int test = roomdelete(test_ptr, '\0'); cout << test << endl; return 0; } int roomdelete(room * room_ptr, char coord) { char coordinate[num_doors] = {'n', 's', 'e', 'w'}; char coordinate_opposite[num_doors] = {'s', 'n', 'w', 'e'}; char coord_opp = '\0'; // call function on remaining rooms if(coord == '\0') // beginning/initial room { for(int = 0; < num_doors; i++) { switch (coordinate[i]) { case 'n': { if(room_ptr->north != null) roomdelete(room_ptr->north, 'n'); break; } case 's': { if(room_ptr->south != null) roomdelete(room_ptr->south, 's'); break; } case 'e': { if(room_ptr->east != null) roomdelete(room_ptr->east, 'e'); break; } case 'w': { if(room_ptr->west != null) roomdelete(room_ptr->west, 'w'); break; } default: cout << "there error deallocating room @ location: " << room_ptr << endl; } } // delete current room if(room_ptr->north == null && room_ptr->south == null && room_ptr->east == null && room_ptr->west == null) { cout << "deleting room @ location: " << room_ptr << endl; delete room_ptr; } else return 2; // outward rooms have not been deleted yet } else // recursion { // sets value door won't handed delete function for(int j = 0; j < num_doors; j++) { if(coord == coordinate[j]) coord_opp = coordinate_opposite[j]; } if(coord_opp == '\0') { cout << "an error occurred while setting value of opposite coordinate.\n"; return 1; } // call function on remaining rooms for(int k = 0; k < num_doors; k++) { if(coordinate[k] != coord_opp) // avoid backtracking (which cause infinite recursion) { switch (coordinate[k]) { case 'n': { if(room_ptr->north != null) roomdelete(room_ptr->north, 'n'); break; } case 's': { if(room_ptr->south != null) roomdelete(room_ptr->south, 's'); break; } case 'e': { if(room_ptr->east != null) roomdelete(room_ptr->east, 'e'); break; } case 'w': { if(room_ptr->west != null) roomdelete(room_ptr->west, 'w'); break; } default: cout << "there error deallocating room @ location: " << room_ptr << endl; } } } // delete connection (ptr's) between current room , previous switch(coord) { case 'n': { room_ptr->south->north = null; room_ptr->south = null; } case 's': { room_ptr->north->south = null; room_ptr->north = null; } case 'e': { room_ptr->west->east = null; room_ptr->west = null; } case 'w': { room_ptr->east->west = null; room_ptr->east = null; } default: cout << "there problem severing connection room @ location: " << room_ptr << endl; } // delete current room if(room_ptr->north == null && room_ptr->south == null && room_ptr->east == null && room_ptr->west == null) { cout << "deleting room @ location: " << room_ptr << endl; delete room_ptr; } else return 3; // outward rooms have not been deleted yet } return 0; // successful in deallocating entire complex }
i don't understand algorithm, can tell failing.
switch (coord) { case 'n':{ room_ptr->south->north = null; room_ptr->south = null; } case 's':{ room_ptr->north->south = null; // <-- program fails here room_ptr->north = null; }
room_ptr->north @ moment null pointer , writing @ location not allowed to.
maybe don't understand switch statements? has called "fall-through" behavior , i.e. doesn't break out because new case, find place start executing code , keep executing until hits "}" or finds explicitly written "break;" in it's way.
Comments
Post a Comment