Thread: help with code.....

  1. #1
    Registered User
    Join Date
    Mar 2006

    help with code..... (graph with linked list implementation)

    ok... here is my 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;
    	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");
    void initRoads(void){ 
    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;
    		if (current->place == to)
          return current->distance;
    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:

    Last edited by blur_guy; 03-19-2006 at 10:13 AM.

  2. #2
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Umeċ, Sweden
    Change your findRoadDistance function so that instead of returning the distance it keeps adding the distance from each point into variable as the list gets traversed until it reaches the destination and then return that variable which is the sum of distances. I am not sure how your list is set up but if it's topology could be seen as a sort of map beware so that your code doesn't walk around in circles when trying to find it's destination (i.e. infinite loop).

    road = (struct roadNode *) malloc(sizeof (struct roadNode));
    Don't cast malloc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM
Website Security Test