qq_41938244
qq_41938244
2019-01-02 22:59

求帮助!C语言 图 邻接表文件怎么读取

  • c语言

结构体是这个:
#define MAXSIZE 11
typedef struct node
{
int dizhi;
int weiquan;
struct node *next;
}NODE,NN[MAXSIZE];
typedef struct
{
int NO;
char name[50];
char xingxi[500];
NODE *head;
}JieDian;
typedef struct
{
JieDian jiedian[MAXSIZE];
int ding;
int bian;
}TU;

TU duqu()            //文件读取
{
    TU *g;
    g=(TU *)malloc(sizeof(TU));
    g->bian=0;
    g->ding=0;
    JieDian *p;
    node *q,*t;
    FILE *fp;
    fp=fopen("tu.txt","rt");
    if(fp==NULL)
        printf("没能成功打开文件!(文件不存在)\n");
    else
    {
        while(!feof(fp))  
        {
            q=(node *)malloc(sizeof(node));
            p=(JieDian *)malloc(sizeof(JieDian));
            fscanf(fp,"%d %s:%s",&p->NO,p->name,p->xingxi);
            t=q=g->jiedian[g->ding].head;
            g->ding++;
            do
            {
                fscanf(fp,"->%d(%dkm) ",&q->dizhi,&q->weiquan);
                t=q;
                q=q->next;
                g->bian++;
            }while(q!=NULL);
            t->next=NULL;           
        }

    }
    printf("\t\t文件中已有%d个顶点,%d条边\n",g->ding,g->bian);
    return (*g);
    fclose(fp);
}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

0条回答