?? 9-11.c
字號:
#include "stdio.h"
#define MAX_VERTICES 100
typedef struct node *node_pointer;
typedef struct node {
int vertex;
node_pointer link;
};
typedef struct hd{ int count;
node_pointer link;
} hdnodes;
hdnodes graph[ MAX_VERTICES ];
void topsort(hdnodes graph[ ],int n)
{ int i,j,k,top;
node_pointer ptr;
/* create a stack of vertices with no predecessors*/
top = -1;
for (i = 0; i < n; i++)
if (!graph[i].count){
graph[i].count = top;
top = i;
}
for (i = 0; i < n; i++)
if (top = -1){
printf("網絡存在循環,拓撲排序結束\n");
exit(1);
}
else{
j = top; /* unstack a vertex */
top = graph[top].count;
printf("v%d",j);
for (ptr = graph[j].link; ptr; ptr = ptr->link){
/*decrease the count of the successor vertices of j */
k = ptr->vertex;
graph[k].count--;
if ( !graph[k].count){ /* add vertex k to the stack */
graph[k].count = top; top = k;
}
}
}
}
void main(void)
{
topsort(graph,10);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -