主题:求助(急):编写一个程序执行这个图的拓扑排序?(已经有排序算法)
#include <stdio.h>
#include <stdlib.h>
typedef struct _T_edge edge_t;
typedef struct _T_vertex vertex_t;
struct _T_edge {
vertex_t *Src, *Dest;
edge_t *Next;
};
struct _T_vertex {
char *Name;
int Visited;
edge_t *Adj_lst;
vertex_t *Next;
};
typedef vertex_t *St_elem_t;
void T_sort(stack_t *S, vertex_t *V)
{ // output sorted vertices to stack S,
// first vertex in topological order is output last.
if ( !V->Visited ) {
edge_t *E;
V->Visited = 1;
for ( E = V->Adj_lst; E; E = E->Next )
T_sort(S, E->Dest);
St_Push(S, V);
}
}
void Top_sort(vertex_t **G)
{
vertex_t *V, **R;
stack_t *S = St_Create();
for ( V = *G; V; V = V->Next )
V->Visited = 0;
for ( V = *G; V; V = V->Next )
T_sort(S, V);
for ( R = G; !St_Is_empty(S); R = &(*R)->Next )
*R = St_Pop(S);
*R = 0;
St_Destroy(S);
}
…………
谢谢大家……
初学不大看的懂这个如何测试……
……
#include <stdlib.h>
typedef struct _T_edge edge_t;
typedef struct _T_vertex vertex_t;
struct _T_edge {
vertex_t *Src, *Dest;
edge_t *Next;
};
struct _T_vertex {
char *Name;
int Visited;
edge_t *Adj_lst;
vertex_t *Next;
};
typedef vertex_t *St_elem_t;
void T_sort(stack_t *S, vertex_t *V)
{ // output sorted vertices to stack S,
// first vertex in topological order is output last.
if ( !V->Visited ) {
edge_t *E;
V->Visited = 1;
for ( E = V->Adj_lst; E; E = E->Next )
T_sort(S, E->Dest);
St_Push(S, V);
}
}
void Top_sort(vertex_t **G)
{
vertex_t *V, **R;
stack_t *S = St_Create();
for ( V = *G; V; V = V->Next )
V->Visited = 0;
for ( V = *G; V; V = V->Next )
T_sort(S, V);
for ( R = G; !St_Is_empty(S); R = &(*R)->Next )
*R = St_Pop(S);
*R = 0;
St_Destroy(S);
}
…………
谢谢大家……
初学不大看的懂这个如何测试……
……