# Include <iostream> using namespace std; # define MAX_NODE 30 # define MAX_INFO 10 bool isOutput [MAX_NODE]; // record whether the node outputs struct ListNode {ListNode () {next = NULL;} int position; ListNode * next;}; struct VertexNode {VertexNode () {head = NULL; inDegree = 0;} int currentPosition; // int inDegree of the current node's array in the graph storage structure; // The node's inbound char info [MAX_INFO]; // The node's information ListNode * head; // The first node of the adjacent node}; struct Graphic {int vertex, edge; VertexNode vers [M AX_NODE] ;}; void createGraphic (Graphic & graphic) {cout <"Enter the number of nodes and edges:"; cin> graphic. vertex> graphic. edge; cout <"Enter the node information:" <endl; int I; for (I = 0; I <graphic. vertex; I ++) {cout <"Enter the" <I <"Node information:"; cin> graphic. vers [I]. info;} cout <"Enter edge information:" <endl; int first = 0; int second = 0; for (I = 0; I <graphic. edge; I ++) {cout <"Enter the" <I <"edge information:"; cin> first> second; listNode * temp = new ListNode; temp-> position = second; temp-> next = graphi C. vers [first]. head; graphic. vers [first]. head = temp; ++ graphic. vers [second]. inDegree;} for (I = 0; I <graphic. vertex; I ++) {isOutput [I] = false;} for (I = 0; I <graphic. vertex; I ++) {graphic. vers [I]. currentPosition = I ;}} void printGraphic (Graphic graphic) {int I; ListNode * temp = NULL; for (I = 0; I <graphic. vertex; I ++) {cout <graphic. vers [I]. info <"--->"; temp = graphic. vers [I]. head; if (! Temp) {cout <"NULL";} while (temp) {cout <temp-> position <""; temp = temp-> next ;} cout <endl ;}for (I = 0; I <graphic. vertex; I ++) {cout <"Node" <I <"inbound:"; cout <graphic. vers [I]. inDegree <endl ;}cout <endl ;}vertexnode * findZeroInDegreeNode (Graphic & graphic) {int I; for (I = 0; I <graphic. vertex; I ++) {if (graphic. vers [I]. inDegree = 0) {graphic. vers [I]. inDegree = 1; return & graphic. vers [I] ;}} return NULL;} void TopologicalSortForVert Ex (Graphic & graphic, int position) {ListNode * head = graphic. vers [position]. head; while (head) {if (! IsOutput [head-> position]) {cout <graphic. vers [head-> position]. info <"; isOutput [head-> position] = true;} TopologicalSortForVertex (graphic, head-> position); head = head-> next ;}} void TopologicalSort (Graphic & graphic) {VertexNode * zeroNode = findZeroInDegreeNode (graphic); if (! ZeroNode) return; if (! IsOutput [zeroNode-> currentPosition]) {cout <zeroNode-> info <""; isOutput [zeroNode-> currentPosition] = true;} TopologicalSortForVertex (graphic, zeroNode-> currentPosition); TopologicalSort (graphic);} void main () {Graphic myGraphic; createGraphic (myGraphic); printGraphic (myGraphic); TopologicalSort (myGraphic );}