用完整的C语言编写单链表的长度
#defineMAX_VERTEX_NUM20typedefstructArcBox{Inttailvex,headvex;StructArcBox*hlink,* }ArcBox;//定义弧tpedefstructvexnode{VertexTypedata;ArcBox*firstin,*firstout;}VexNode;//定义定点typedefstruct{VexNodeXlist[MAX_VERTEX_NUM];Intvexnumarcnum;}OLGraph;//定义图LocatFirstNull(OLGraph&G,inti,intn)//查找第一个没有前驱的顶点,即第一个...全部
#defineMAX_VERTEX_NUM20typedefstructArcBox{Inttailvex,headvex;StructArcBox*hlink,* }ArcBox;//定义弧tpedefstructvexnode{VertexTypedata;ArcBox*firstin,*firstout;}VexNode;//定义定点typedefstruct{VexNodeXlist[MAX_VERTEX_NUM];Intvexnumarcnum;}OLGraph;//定义图LocatFirstNull(OLGraph&G,inti,intn)//查找第一个没有前驱的顶点,即第一个firstin为空的顶点,返回其位置i,n为循环次数,等于初始时顶点的个数。
{for(k=0;k
XList[i]。firstin。headvex==v2){j=i;returntrue;}}retrunfalse;}TopSort(OLGraph&G){ArcBox*p1,*p2,*pt;inti,j,n;n= xnum;while(LocatFirstNull(G,i,n)){p1=G。
XList[i]。firstout;free(G。XList[i]);//释放数据,保留位置,可以使用无效数据填充;因为若把位置删除,会使图的存储变乱。在释放前加printfG。XList[i]。
data,最后得到该图拓扑序列。 xnum--;while(p1){if(LocateFirstinheadvex(G,j,*p1。headvex,n)){p2=G。XList[j]。firstin;while(*p2。
hink。tailvex!=*p1。tailvex)p2=p2。hlink;p2。hlink=h1。hlink;//修改同尾链域的指针,使原来指向要删除弧的指针,指向下一个或空。}t=p1; ink;free(t);//释放以删除顶点为尾的弧; cnum--;}}if( xnum==0)printf此图没有回路!;elseprintf此图有回路;}。
收起