私はこれを何のために作るのかという質問を受けました、私は実装だけを見ました
for(i=0;nlist[i].nid!=0;i++)
{}
nlist[i].nid=n_id;
nlist[i].add=temp;
nlistは型構造の配列です
構造体
struct node
{
int id;
struct node *l[MAX_CONN+1];
int cost[MAX_CONN+1];
struct node **next;
int *mincost;
};
typedef struct node nodes;
struct record
{
int nid;
struct node *add;
};
typedef struct record records;
records nlist[MAX_NODES+1]={0};
フル機能
FILE *f; int d; int i=0,j=0,n_id,n_cost; nodes *temp=0,*temp1=0; if((f=fopen("graph.txt","r"))== NULL) { printf("Error opening file.\n"); exit(1); } memset(nlist, 0, sizeof(struct record) * MAX_NODES); count=0; do /*first get the id and address of all nodes*/ { fscanf(f,"%d",&n_id); for(i=0;nlist[i].nid!=0;i++) { if(n_id==nlist[i].nid) { printf("Id already exists."); return; } } temp=(nodes *)malloc(sizeof(nodes)); if (temp == 0) { printf("ERROR: Out of memory\n"); return; } memset(temp, 0, sizeof(struct node)); temp->id=n_id; temp->l[MAX_CONN+1]=0; temp->cost[MAX_CONN+1]=0; for(i=0;nlist[i].nid!=0;i++) {} nlist[i].nid=n_id; nlist[i].add=temp; count++; while((d=fgetc(f)!=';')) {} }while((d=fgetc(f))!=EOF); rewind(f); for(i=0;i<count;i++) /*now get the information of all nodes connections.*/ { fscanf(f,"%*d"); temp=nlist[i].add; while((d=fgetc(f)!=';')) { fscanf(f,"%d-%d",&n_id,&n_cost); for(j=0;nlist[j].nid!=0;j++) { if(nlist[j].nid==n_id) { temp1=nlist[j].add; break; } } for(j=0;temp->cost[j]!=0;j++) {} temp->cost[j]=n_cost; temp->l[j]=temp1; } } fclose(f);