please help i think d error is in inputtin d values into d linked list! D files are attached
TriniBabe 0 Newbie Poster
BW900 pos Arima 2.2 7
jk11 Here there 90 1200
xbxb Sando Gran 66 120
END
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
# define MaxWordSize 5
# define MaxDests 50
typedef struct edge
{
char flightNo[MaxWordSize + 1];
char destinationPort[MaxWordSize +1];
double cost;
double duration;
struct edge*next;
}Edge, * EdgePtr;
typedef struct Vertex
{
char embarkmentPort[MaxWordSize +1];
EdgePtr Destinations;
}Vertex;
EdgePtr newEdge (char f[MaxWordSize + 1],double d, double c, char des[MaxWordSize + 1])
{
EdgePtr p = (EdgePtr) malloc (sizeof(Edge));
strcpy(p->flightNo,f);
strcpy(p->destinationPort,des);
p->duration = d;
p->cost = c;
p->next = NULL;
return p;
}
Vertex newVertex (char emb[MaxWordSize + 1])
{
Vertex temp;
strcpy(temp.embarkmentPort,emb);
temp.Destinations = NULL;
return temp;
}
typedef struct graph
{
int numV;
Vertex vertices[MaxDests + 1];
}*Graph;
Graph newGraph()
{
Graph p = (Graph) malloc (sizeof(struct graph));
p->numV = MaxDests;
return p;
}//end newGraph
int main(){
int buildGraph(FILE * ,Graph G,int );
void printGraph(Graph , int );
int a,n=0;
FILE * in1 = fopen("flights.txt","r");
if(in1==NULL)
{
printf("File could not be found.\n\n");
exit(1);
}
Graph G = newGraph();
a = buildGraph(in1,G,n);
printGraph(G,a);
fclose(in1);
system("pause");
return 0;
}//end main
int buildGraph(FILE * in1,Graph G,int n)
{
EdgePtr addInPlace(EdgePtr,char[], double, double, char[]);
int i;
char temp[MaxWordSize + 1];
char ePort[MaxWordSize + 1], dPort[MaxWordSize + 1];
double d,c;
EdgePtr top = NULL;
fscanf(in1,"%s",&temp);
while(strcmp("END",temp)!=0)
{
fscanf(in1, "%s",&ePort);
fscanf(in1, "%s",&dPort);
fscanf(in1, "%lf",&d);
fscanf(in1, "%lf",&c);
for(i=0; i <= n; i++)
{
if(strcmp(G->vertices[i].embarkmentPort,ePort)!=0)
{
n++;
G->vertices[n] = newVertex ("");
strcpy(G->vertices[n].embarkmentPort,ePort);
top = addInPlace(G->vertices[n].Destinations,temp, d, c, dPort);
}//end if
else
{
top = addInPlace(G->vertices[i].Destinations,temp, d, c, dPort);
}//end else
}//end for
fscanf(in1,"%s",&temp);
}//end while
return n;
}//end buildGraph
//Adding the destination ports for each embarkment port in alphabetical order
EdgePtr addInPlace(EdgePtr top,char temp[MaxWordSize +1], double d,double c, char dPort[MaxWordSize +1])
{
EdgePtr newP,prev,curr;
newP = newEdge (temp, d, c, dPort);
prev = NULL;
curr = top;
while((curr!= NULL) && (strcmp(dPort,curr->destinationPort) > 0))
{
prev = curr;
curr = curr->next;
}//end while
if (prev == NULL)
{
newP->next = top;
return newP;
}//end if
newP->next = curr;
prev->next = newP;
return top;
}//end addInPlace
void printGraph(Graph G, int n)
{
int j;
printf("Embarkment Port Destination Ports Flight No Duration Cost\n\n");
for (j=1; j<= n; j++)
{
printf("%-15s",G->vertices[j].embarkmentPort);
while(G->vertices[j].Destinations!= NULL)
{
printf("%s %s" ,G->vertices[j].Destinations->destinationPort, G->vertices[j].Destinations->flightNo);
printf("%lf .%2lf",G->vertices[j].Destinations->duration,G->vertices[j].Destinations->cost);
G->vertices[j].Destinations = G->vertices[j].Destinations ->next;
}//end while
} //end for
printf("\n");
}//end printgraph
Nick Evan 4,005 Industrious Poster Team Colleague Featured Poster
This is no way to ask a question. What the hell are we supposed to do with "i think d error is in inputtin d values into d linked list" ?
Post your code using code tags (don't just upload some files) and then tell us
1. what the code is supposed to do (input/ouput)
2. What the code is actually doing
3. (optional) tell us where you think the problem is.
Here's a sort of 'forum-manual', you might benefit from reading it.
vmanes 1,165 Posting Virtuoso
Also, writing in something approaching proper English is appreciated. Text- or leet-speak is generally not appreciated.
TriniBabe 0 Newbie Poster
ok!
the code reads i in data about each flight(given on a seperate line 4 each flight) so i hav 2 build a graph 2 find shortest distance!
so i create a vertex that stores a given startin pt and a linked list that stores all its possible end pts along with cost,time to get from start vertex 2 en vertex!
but i dont no wat i did wrong?????
int buildGraph(FILE * in1,Graph G,int n)
{
EdgePtr addInPlace(EdgePtr,char[], double, double, char[]);
int i;
char temp[MaxWordSize + 1];
char ePort[MaxWordSize + 1], dPort[MaxWordSize + 1];
double d,c;
EdgePtr top = NULL;
fscanf(in1,"%s",&temp);
while(strcmp("END",temp)!=0)
{
fscanf(in1, "%s",&ePort);
fscanf(in1, "%s",&dPort);
fscanf(in1, "%lf",&d);
fscanf(in1, "%lf",&c);
for(i=0; i <= n; i++)
{
if(strcmp(G->vertices[i].embarkmentPort,ePort)!=0)
{
n++;
G->vertices[n] = newVertex ("");
strcpy(G->vertices[n].embarkmentPort,ePort);
top = addInPlace(G->vertices[n].Destinations,temp, d, c, dPort);
}//end if
else
{
top = addInPlace(G->vertices[i].Destinations,temp, d, c, dPort);
}//end else
}//end for
fscanf(in1,"%s",&temp);
}//end while
return n;
}//end buildGraph
Edited by Reverend Jim because: Fixed formatting
Nick Evan 4,005 Industrious Poster Team Colleague Featured Poster
If your to lazy to click the link(s) I gave you, then I'm to lazy to try and help.
Use. Code. Tags.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.