拓扑排序(C语言 邻接矩阵存储)

#include “stdafx.h”

#include”malloc.h”

#define MAX_VERTEX_NUM 20

#define STACK_INIT_SIZE 100

#define STACKINCREMENT 10

#define OVERFLOW -2

#define OK 1

#define ERROR -1

#define FALSE 0

typedef int VertexType ;

typedef int InfoType;

typedef int Status;

typedef int SElemType;

typedef int Status;

int indegree[20];

typedef struct{

SElemType *base;

SElemType *top;

int stacksize;

}SqStack;

typedef struct ArcNode{

int adjvex;

struct ArcNode *nextarc;

InfoType *info;

}ArcNode;

typedef struct VNode{

VertexType data;

ArcNode *finrstarc;

}VNode,AdjList[MAX_VERTEX_NUM];

typedef struct{

AdjList vertices;

int vexnum,arcnum;

int kind;

}ALGraph;

ALGraph G; 

Status LocateVex(int v){

for(int i=0;i<G.vexnum;i++)

{

if(G.vertices[i].data==v)

return i;

}

return ERROR;

}

Status InitStack(SqStack &S){

S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));

if(!S.base) return OVERFLOW;

S.top=S.base;

S.stacksize=STACK_INIT_SIZE;

return OK;

}

Status GetTop(SqStack S,SElemType &e){

if(S.base==S.top)

return ERROR;

e=*(S.top-1);

return OK;

}

Status Push(SqStack &S,SElemType e)

{

if(S.top-S.base>=S.stacksize)

{

S.base=(SElemType *)realloc(S.base,

(S.stacksize+STACKINCREMENT)*sizeof(SElemType));

if(!S.base)

return OVERFLOW;

S.top=S.base+S.stacksize;

S.stacksize+=STACKINCREMENT;

}

*S.top++=e;

return OK;

}

Status Pop(SqStack &S,SElemType &e){

if(S.base==S.top)

return ERROR;

e=*–S.top;

return OK;

}

Status StackEmpty(SqStack S)

{

if(S.base==S.top)

return OK;

return FALSE;

}

Status CreateDG(){

printf(“please input G.vexnum=”);

scanf(“%d”,&G.vexnum);

printf(“please input G.arcnum=”);

scanf(“%d”,&G.arcnum);

for(int i=0;i<G.vexnum;i++) {

printf(“please input G.vertices[%d].data=”,i);

scanf(“%d”,&G.vertices[i].data);

}

for(int i=0;i<G.vexnum;i++) G.vertices[i].finrstarc=NULL;

printf(“please input G.arcnum arcs\n”);

for(int k=0;k<G.arcnum;k++){

int i,j,v1,v2;

printf(“”);

scanf(“%d%d”,&v1,&v2);

i=LocateVex(v1);

j=LocateVex(v2);

indegree[j]++;

ArcNode *p=(ArcNode *)malloc(sizeof(ArcNode));

ArcNode *q=(ArcNode *)malloc(sizeof(ArcNode));

if(!G.vertices[i].finrstarc)

G.vertices[i].finrstarc=p;

else{

for(q=G.vertices[i].finrstarc;q->nextarc;q=q->nextarc);

q->nextarc=p;

}

p->adjvex=j;

p->nextarc=NULL;

}

printf(“createDG is:\n”);

for(int i=0;i<G.vexnum;i++) {

printf(“G.vertices[%d].data=%d\n”,i,G.vertices[i].data);

}

printf(“The edge of a directed graph is: “);

ArcNode *r=(ArcNode *)malloc(sizeof(ArcNode));

for(int i=0;i<G.vexnum;i++)

for(r=G.vertices[i].finrstarc;r;r=r->nextarc){

printf(“%d–>%d “,i+1,r->adjvex+1);

}

return OK;

}//创建图 

Status TopologicalSort(){

SqStack S;

InitStack(S);

for(int i =0;i<G.vexnum;i++)

if(indegree[i]==0) Push(S,i);

int count=0;

printf(“\nTopological sequence is:”);

while(!StackEmpty(S)){

int i;

Pop(S,i);

printf(“%d “,G.vertices[i].data);

count++;

ArcNode *p=(ArcNode *)malloc(sizeof(ArcNode));

for(p=G.vertices[i].finrstarc;p;p=p->nextarc){

indegree[p->adjvex]–;

if(indegree[p->adjvex]==0)

Push(S,p->adjvex);

}

}

printf(“\n”);

if(count<G.vexnum)

return ERROR;

return OK;

}//拓扑排序

int _tmain(int argc, _TCHAR* argv[])

{

CreateDG();

ArcNode *p;

int i;

i=TopologicalSort();

if(i==1)

printf(“Topology sorting successful,The directed graph has no ring\n”);

else

printf(“Topology sorting failed,The directed graph has rings\n”);

return 0;

}

    原文作者:拓扑排序
    原文地址: https://blog.csdn.net/fengzhongzhui_feng/article/details/75449073
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞