ok... here is my code....
Code:
#include <stdio.h>
#include <stdlib.h>
#define NAMESIZE 40
#define NOPATH -1.0
#define AHMAD 0
#define GURWIND 1
#define AHFATT 2
#define BOB 3
#define NUMPLACE 4
struct placeNode{
char name[NAMESIZE];
struct roadNode *road;
};
struct roadNode{
float distance;
struct placeNode *place;
struct roadNode *next;
};
struct placeNode places[NUMPLACE] = {
{"Ahmad's Mini Market", NULL},
{"Gurwinder Singh's Clinic", NULL},
{"Ah Fatt's Auto Repair", NULL},
{"Bob's Restaurant", NULL}
};
void initRoads(void);
void link(struct placeNode *from,
float distance, struct placeNode *to);
float findRoadDistance(struct placeNode *from,
struct placeNode *to);
int main(void){
int fromIndex, toIndex;
struct placeNode *from, *to;
float distance;
initRoads();
printf("From:"); scanf("%d",&fromIndex);
printf("To:"); scanf("%d",&toIndex);
from = &places[fromIndex];
to = &places[toIndex];
printf("From %s to %s ", from->name,to->name);
distance = findRoadDistance(from,to);
if(distance!=NOPATH)printf("it is %2.1f km\n",distance);
else printf(": no direct road.\n");
return(0);
}
void initRoads(void){
link(&places[AHMAD],3.0,&places[GURWIND]);
link(&places[AHMAD],5.1,&places[AHFATT]);
link(&places[GURWIND],2.0,&places[AHMAD]);
link(&places[AHFATT],7.2,&places[BOB]);
link(&places[AHFATT],3.3,&places[GURWIND]);
link(&places[AHFATT],4.9,&places[AHMAD]);
link(&places[BOB],7.2,&places[AHFATT]);
link(&places[BOB],4.1,&places[GURWIND]);
}
void link(struct placeNode *from,
float distance,struct placeNode *to){
struct roadNode *road;
road = (struct roadNode *)
malloc(sizeof (struct roadNode));
road->distance = distance;
road->place = to;
road->next = from->road;
from->road = road;
}
float findRoadDistance(struct placeNode *from,
struct placeNode *to){
struct roadNode *current;
for (current = from->road; current;
current=current->next){
if (current->place == to)
return current->distance;
}
return(NOPATH);
}
since there is no path from ahmad's mini market to bob's restaurant.... how am i able to calculate the distance from ahmad's mini market to bob's restaurant??
as a default, i have state that since there is no direct path from those places, i would just print "no direct path"
but how am i to alter this code to enable me to calculate the distance to another place with no direct link? (lets say from ahmad's mini market, passing by ahfatt's auto repair and to bob's restaurant?
thanks in advance!
btw, here is the map:
map