c语言实现拓扑排序

/* * c语言实现拓扑排序. */ #include #include #define MAX_VERTEX_NUM 30 #define INFINITY 0 /** 用链表实现栈 */ typedef int DataType; typedef struct stack { DataType data; struct stack *next; }Stack; Stack *S=NULL; void InitStack() { S=(Stack *)malloc(sizeof(Stack)); S->next = NULL; } int StackEmpty() { return S->next == NULL; } void Push(DataType value) { Stack *new_node = NULL; if((new_node=(Stack *)malloc(sizeof(Stack)))==NULL) { printf("memory alloc error!/n"); } new_node->data = value; new_node->next = S->next; S->next = new_node; } void Pop() { Stack *first_node; if(StackEmpty()) { printf("Stack is empty!/n"); return; } first_node=S; S = first_node->next; free(first_node); } DataType GetTop() { return S->next->data; } void StackPrint() { Stack *stack=NULL; printf("The elements of stack are: /n"); for(stack=S->next; stack!=NULL; stack=stack->next) printf("%d/n",stack->data); } /** 用邻接矩阵实现图的存储 */ typedef struct ArcCell { int power; char info; //have not used! }ArcCell; typedef struct { char vexs[MAX_VERTEX_NUM]; ArcCell arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; int vexnum, arcnum; }MGraph; /** 定位顶点位置的函数 */ int LocateVex(MGraph *G, char c) { for(int i=0; ivexnum; ++i) { if(c==G->vexs[i]) return i; } } void PrintVex(MGraph *G, int index) { putchar(G->vexs[index]); } /** 有向画的创建 */ void CreateDG(MGraph *G) { printf("请输入顶点数目按回车继续: /n"); scanf("%d", &G->vexnum); getchar(); /*用来吸收键入的回车符*/ printf("请输入弧数目按回车继续: /n"); scanf("%d", &G->arcnum); getchar(); /*用来吸收键入的回车符*/ printf("请输入各顶点的字符表示,如/"abcdef/",按回车继续: /n"); for(int k=0; kvexnum; ++k) { scanf("%c", &G->vexs[k]); //printf("%c",G->vexs[k]); } getchar(); /*用来吸收键入的回车符*/ //char temp; temp = getchar(); /*用来做测试的语句*/ //for(int n=0; nvexnum; ++n) printf("%c", G->vexs[n]); /*用来做测试 的语句*/ for(int i=0; ivexnum; ++i) for(int j=0; jvexnum; ++j) G->arcs[i][j].power = INFINITY; char vex1, vex2; int power; for(int m=0; marcnum; ++m) { printf("输入一条边依附的顶点,如/"a,b/"按回车继续: /n "); scanf("%c,%c", &vex1, &vex2); int i = LocateVex(G, vex1), j = LocateVex(G, vex2); //printf("%d,%d/n", i, j); /*用来做测试的语句*/ G->arcs[i][j].power = 1; getchar(); /*用来吸收键入的回车符*/ } //printf("%d,%d",G->arcnum,G->vexnum); //printf("/n%d", G->arcs[0][1].power); } void PrintMatrix(MGraph *G) { for(int i=0; ivexnum; i++) { printf("/n"); for(int j=0; jvexnum; j++) printf("%d", G->arcs[i][j].power); } } int FindInDegree(MGraph *G, int i) { int indegreecount = 0; for(int j=0; jvexnum; ++j) { if(G->arcs[j][i].power == 1) indegreecount++; } return indegreecount; } /** 用来判断顶点的下一个邻接点 */ int NextVex(MGraph *G, int i, int j) { for(; jvexnum; ++j) { if(G->arcs[i][j].power == 1) return j; } return G->vexnum; } /** 拓扑排序函数 */ void TopologicalSort(MGraph *G) { int indegree[G->vexnum]; for(int i=0; ivexnum; ++i) { indegree[i] = FindInDegree(G, i); //printf("%d/n", indegree[i]); /*用来做测试的语句*/ } InitStack(); for(int i=0; ivexnum; ++i) { if(!indegree[i]) Push(i); } int count = 0; //printf("%d/n", S->next->data); /*用来做测试的语句*/ printf("Topological sort: /n"); while(!StackEmpty()) { int t = GetTop(); Pop(); PrintVex(G, t); printf("->"); count++; for(int j=NextVex(G, t, 0); jvexnum ; j=NextVex(G ,t,++j)) { if(!--indegree[j]) Push(j); } } if(countvexnum) printf("该有向图有回路"); } int main() { MGraph mgraph, *G; G = &mgraph; CreateDG(G); //PrintMatrix(G); /*用来做测试的语句*/ TopologicalSort(G); return 0; } //如输入 //6 //8 //abcdef //a,b //a,c //a,d //b,c //c,e //c,f //d,e //e,f //输出结果为 //a->d->b->c->e->f->

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(c语言实现拓扑排序)