c - Linking and assigning struct elements to random nodes -
this problem:
basically, given large file (think 20000+ lines of text) contains following: line 1:
<number_of_nodes> <number of edges>
rest:<source> <destination> <cost>
goal figure out memory efficient way create structure(s) allow associate nodes connected nodes. these connections bidirectional.the user should allowed input location/node , program must output neighbors of node , corresponding cost input node each of neighbors.
*note: |large files| contain 25000 lines of text in format
integer integer integer
here's supposed output:
number of nodes 25000 enter node examine: 0 neighbors of node 0 are: node 1 cost 450 node 100 cost 1130 enter node examine:0 neighbors of node 0 are: node 4 cost 850 node 91 cost 304 enter node examine: 0 neighbors of node 0 are: node 3 cost 230 node 11 cost 330 enter node examine: exit
and here's i've tried:
#include <stdio.h> #include <stdlib.h> struct graph { int src; int dest; int cost; }pathedge; int main() { int nlines, i,v; char r; int ecost,ebegin, eend; scanf(“%s”,&nlines); pathedge[nlines + 1]; for(i = 0; < nlines; i++) { scanf("%d, %d, %d\n", &ebegin, &eend, &ecost); edges[i].begin = ebegin; edges[i].end = eend; edges[i].cost = ecost; struct town { struct town *north; int name[25000]; }; struct town *root, *current; root = (struct town *) malloc (sizeof (struct town)); root->north = null; strcpy (root->name,ecost); current = (struct town *) malloc (sizeof (struct town)); current->north = root; strcpy (current->name, ecost); strcpy(ecost,”node %d”,i); } printf(“%d”,sizeof(int name)); printf(“please enter node want examine. if want exit, please press ‘x’.\n”); scanf(“%c”,&r); switch(r) { case ‘x’: case ‘x’: printf(“you entered wrong value. gomen. try again.\n”); break; default: if(0<r<25000) { printf(“you have accessed node %d\n”,r); printf(“its neighboring nodes %d\n %d\n”,ecost[nlines+1],ecost[nlines+2]); } else { printf(“invalid input again. please try again. thanks\n”); } break; } return 0; }
Comments
Post a Comment