計算機應用專業(yè)上機考試輔導三

字號:

編一C程序,它能根據(jù)讀入的數(shù)據(jù)構造有向圖G,并輸出G的DFS遍歷序列(從V0開始),圖的輸入形式為n V0 Vi0 V1 Vi1 V2 Vi2...Vi Vin -1 -1(-1,-1為輸入結束標記,其余的值都>=0且n>0。
    (注:程序的可執(zhí)行文件名必須是 e3.exe)
    #include
    typedef enum {False,True} Boolean;
    int G[100][100];
    int n;
    void CreatG() /*建立圖的鄰接矩陣G[][]*/
    {int i,j;
    printf("Input the number of the node:");
    scanf("%d",&n);
    printf("\n");
    for (i=0;i for (j=0;j G[i][j]=0;
    do
    { scanf("%d %d",&i,&j);
    G[i][j]=1;
    }while ((i!=-1)&&(j!=-1));
    }
    void TopSort() /*拓撲排序,輸出拓撲序列*/
    { int i,j;
    int degree[100]; /*按照無前驅(qū)頂點優(yōu)先思想,degree[]存放個節(jié)點的入度.*/
    Boolean visited[100],flag=True;
    printf("The Topolgical Order as follow:");
    for (i=0;i { degree[i]=0;
    visited[i]=False;
    }
    printf("\n");
    while(flag==True)
    {
    for (i=0;i for (j=0;j degree[i]=G[j][i]+degree[i];
    i=0;
    while ((i if (i {printf(" %d",i);
    visited[i]=True;
    for(j=0;j {G[i][j]=0; degree[j]=0;}
    }
    else flag=False;
    }
    }
    main()
    { CreatG();
    TopSort();
    }