Thread: Problem with linked list

  1. #1
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798

    Problem with linked list

    Ok, here is what I am attempting.

    I have a map (2D array) of the inside of a building for part of my project. The 'working' areas of this map are corridors etc. This working area is divided into zones and there are interfaces between each zone. So for example there could be a corridor zone and a room zone with the doorway as the interface. The interfaces also double up as waypoints, I just assigned 2 different names in case I got confused. Here are the classes I have contructed:

    Code:
    class waypoints
    {
    private:
    	int *w_coordinates;
    	int number_of_w_squares;
    	zone *zone1;
    	zone *zone2;
    
    public:
    	void set_w_coordinates(int pos_x, int pos_y, int a);
    	void setup(int num_squares, zone &a, zone &b);
    	zone* GetZone1();
    	zone* GetZone2();	
    	~waypoints();
    }w_1,w_2,w_3,w_4;
    
    class zone
    {
    private:
    	int number_of_squares, number_of_interfaces;
    	int *coordinates;
    	
    public:
    	void setup(int a, int b);
    	void set_coordinates(int i, int pos_x, int pos_y);
    	void set_interfaces(waypoints b);
    	bool IsInZone(int x, int y);
    	int GetSquaresNumber();
    	int GetSquareCoordinate(int i);
    	vector <waypoints> interfaces;
    	~zone();
    	
    };
    As I hope can be determined, a zone has a vector of all it's intefaces (A corridor can have many rooms and other corridors attached to it). The waypoints class has 2 pointers to zones (one zone either side).

    I am trying to implement a search algorithm that, given start and end coordinates, will populate a vector of zones with the correct sequence of zones (assuming only one route is possible) to get from start to finish.

    I have begun a function here:

    Code:
    FindZoneRoute(int startx, int endx, int starty, int endy)
    {
    	vector<zone *> zone_route;
    
    	for (vector<waypoints>::iterator wi=GetZone(startx,starty).interfaces.begin(),wi != GetZone(startx,starty).interfaces.end(),wi++)
    	{
    		if (!CompareZones(wi->GetZone1(),&current))
    		{
    			
    		}
    		if (!CompareZones(wi->GetZone2(),&current))
    		{
    			
    		}
    	}
    }
    GetZone() returns the zone containing the coordinates given.
    GetZone1() and GetZone2() return the pointer to zone1 or zone2 respectively held in the zone class.
    CompareZones() returns true if the zones passed into it are the same. The if(!CompareZones()) is used so that I don't end up going backwards.

    Please bear with me if I clearly haven't got a clue or if my code sucks, I don't know if I am in the right forest, yet alone barking up the right tree!

    Any help or guidance would be great, I am lost on this. Thanks
    Last edited by minesweeper; 12-11-2002 at 06:36 AM.

  2. #2
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    I have now changed FindZoneRoute to:

    Code:
    FindZoneRoute(int startx, int endx, int starty, int endy)
    {
    	vector<zone *> zone_route;
    	zone *current = new zone;
    	current = GetZone(startx, posx);
    
    	for(int i=0;!CompareZones(current,GetZone(endx,endy));i++)
    	{
    		
    	for (vector<waypoints>::iterator wi=current.interfaces.begin(),wi != current.interfaces.end(),wi++)
    	{
    		if (!CompareZones(wi->GetZone1(),&current))
    		{
    			current = wi->GetZone1();
    		}
    		if (!CompareZones(wi->GetZone2(),&current))
    		{
    			current = wi->GetZone2();	
    		}
    	}
    	}
    }
    Which I think is more on the right track. It still has no return type cos I'm not sure what it is going to return yet. I'm kinda feeling my way in the dark. Any help would be great, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM