Topological sorted C language source code:
# include
# include
# define m 30
# Define Prprintf
# Define SC Scanf
Typedef struct arcnode {int adjVex; struct arcnode * nextarc;} arcnode, * anode;
TypedEf struct vexNode {int data; int indegree; struct arcnode * firstarc;} vexNode, * vNode, g [m];
Typedef struct lnot {int data; struct lnode * next;} lnot, * link; link l = null;
Void push (int R) {link q; q = (lnot)); Q-> DATA = R; Q-> next = L; l = q;}
CHAR POP (INT R) {Link P; if (l! = null) {P = L; L = L-> Next; R = P-> Data; Free (p); Return R;} else return -1; }
Void Creatgraph (G, INT N) {INT I, J; VNODE q; Anode P; for (i = 1; i <= n; i ) {g [i] .data = i; g [i] .indegree = 0; g [i] .firstarc = null;} Printf ("INPUT THE EDGE: I, J / N"); Scanf ("% D,% D", & I, & J); while (i! = 0) {P = (anode) Malloc (SIZEOF (ArcNode)); p-> adjvex = j; p-> nextarc = g [i] .firstarc; g [i] .firstarc = p; g [j] .indegree ; scanf ("% D,% D", & I, & j);}}
Void Printgraph (G, INT N) {INT I; ANODE P; Pr ("/ Nadjacency List Graph: / N"); Printf ("VexNode Indgree Adjvex / N); for (i = 1; i <= n i ) {PR ("VEX [% D]: [% D]", g [i] .data, g [i] .indegree); p = g [i] .firstarc; while (p) {PR ( "-> [% d]", p-> adjvex); p = p-> nextarc;} PR ("/ n");}}
INT TOPSORT (G, INT N) {Anode P; INT I, K, Count = 0; for (i = 1; i <= n; i ) IF (g [i] .indegree == 0) PUSH (i ); Pr ("/ NAFTER TOPSORT / N:"); While (L) {i = POP (I); Pr ("% D / N", G [i] .data); count ; for (p = g [i] .firstarc; p; p = p-> nextarc) {k = p-> adjvex; if ((- g [k] .indegree) == 0) Push (k);}}} }
Void main () {g g g; int N; CLRSCR (); Pr ("% D", & n); Creatgraph (G, N); PrintGraph (g, n) TOPSORT (G, N);
Big penguin, we will contact it in the future!