#include using namespace std;#define MAX_NODE 30#define MAX_INFO 10bool isOutput[MAX_NODE]; //记录该节点有没有输出struct ListNode{ ListNode(){next=NULL;} int position; ListNode* next; };struct VertexNode{ VertexNode() { head=NULL; inDegree=0; } int currentPosition; //当前节点在图存储结构中数组的位置 int inDegree; //该节点的入度 char info[MAX_INFO]; //该节点的信息 ListNode* head; //该节点相邻节点的第一个节点};struct Graphic{ int vertex,edge; VertexNode vers[MAX_NODE];};void createGraphic(Graphic& graphic){ cout<<"请输入节点数和边数: "; cin>>graphic.vertex>>graphic.edge; cout<<"请输入节点信息:"< >graphic.vers[i].info; } cout<<"请输入边信息:"< >first>>second; ListNode* temp=new ListNode; temp->position=second; temp->next=graphic.vers[first].head; graphic.vers[first].head=temp; ++graphic.vers[second].inDegree; } for(i=0;i "; temp=graphic.vers[i].head; if(!temp) { cout<<"NULL"; } while(temp) { cout< position<<" "; temp=temp->next; } cout< position]) { cout< 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< info<<" "; isOutput[zeroNode->currentPosition]=true; } TopologicalSortForVertex(graphic,zeroNode->currentPosition); TopologicalSort(graphic);}void main(){ Graphic myGraphic; createGraphic(myGraphic); printGraphic(myGraphic); TopologicalSort(myGraphic);}