值得说的一个问题:C语言实现拓扑排序

用的邻接表来存储无向图,然后读取图中每个顶点的入度值存入int数组中,用队列存储入度为0的顶点。


程序如下:

#include
#include
#include

#define MaxSize 20
typedef char VertexType;
typedef struct node  //边表节点
{
int adjvex;
 structnode *next;
}EdgeNode;

typedef struct  //顶点表节点
{
VertexType vertex;
EdgeNode *firstedge;
}VertexNode;

typedef VertexNode AdjList[MaxSize];
typedef struct                
{
AdjList adjlist;
int n,e;
}ALGraph;

//链队列
typedef struct Node{
int data;
struct Node *next;
} QNode;

typedef struct {
QNode *front;
QNode *rear;
}LQueue;

LQueue *Creat_LQueue(){             //创建一个链队列
QNode *r=(QNode *)malloc(sizeof(QNode));
LQueue *s=(LQueue *)malloc(sizeof(LQueue));
r->next=NULL;
s->front=s->rear=r;
return s;
}
void InQueue(LQueue *s,int x){           //入队列
QNode *r;
r=(QNode *)malloc(sizeof(QNode));
r->data=x;
r->next=NULL;
s->rear->next=r;
s->rear=r;
}

int Empty_LQueue(LQueue *s){           //判断队列是否为空
if(s->front==s->rear)
return 1;
else 
return 0;
}

void Out_LQueue(LQueue *s,int x){              //出队列

QNode *r;
if(Empty_LQueue(s)==1)
printf("队列为空\n");
else{
r=s->front->next;
s->front->next=r->next;
x=r->data;
free(r);
if(s->front->next==NULL)
s->rear=s->front;

}
}

void Creat(ALGraph *G){                         //以邻接表的形式创建无向图
int i,j,k;
EdgeNode *s;
printf("读入定点数和边数");
scanf("%d,%d",&G->n,&G->e);

for(i=0;in;i++)
{
fflush(stdin);
printf("建立顶点表");
G->adjlist[i].vertex=getchar();
G->adjlist[i].firstedge=NULL;
}
printf("建立边表\n");
for(k=0;ke;k++)
{
printf("读入(vi-vj)顶点对应序号");
scanf("%d,%d",&i,&j);
s=(EdgeNode* )malloc(sizeof(EdgeNode));
s->adjvex=j;
s->next=G->adjlist[i].firstedge;
G->adjlist[i].firstedge=s;
s=(EdgeNode* )malloc(sizeof(EdgeNode));
s->adjvex=i;
s->next=G->adjlist[j].firstedge;
G->adjlist[j].firstedge=s;
}
}

void print(ALGraph *G){                     //以邻接表的形式输出无向图
int i;

for(i=0;in;i++){
printf("%d->",i);
while(G->adjlist[i].firstedge!=NULL){
printf("%d->",G->adjlist[i].firstedge->adjvex);
G->adjlist[i].firstedge=G->adjlist[i].firstedge->next;
}
printf("\n");
}
}

void FindID(ALGraph *G,int indegree[MaxSize]){       //求各个顶点的入度值
int i;
EdgeNode *p;
for(i=0;in;i++)
indegree[i]=0;
for(i=0;in;i++){
p=G->adjlist[i].firstedge;
while(p!=NULL){
indegree[p->adjvex]++;
p=p->next;
}
}
}

void TopoSort(ALGraph *G){                      //拓扑排序
LQueue *Q; 
int indegree[MaxSize];
int i,count,k;
EdgeNode *p;
FindID(G,indegree);
Q=Creat_LQueue();
for(i=0;in;i++)
if(indegree[i]==0)
InQueue(Q,i);
count=0;
while(Empty_LQueue(Q)!=1){
Out_LQueue(Q,i);
printf("%c",G->adjlist[i].vertex);
count++;
p=G->adjlist[i].firstedge;
while(p!=NULL){
k=p->adjvex;
indegree[k]--;
if(indegree[k]==0)
InQueue(Q,k);
p=p->next;
}
}
if(countn)
printf("该无向图有回路,无法进行拓扑排序.\n");
else
printf("拓扑排序成功!\n");
} 

void main(){
ALGraph *G;
printf("--------------------开始创建图----------------------------\n");
G=(ALGraph* )malloc(sizeof(ALGraph));
Creat(G);
printf("---------------以邻接表的形式输出图----------------------\n");
print(G);
printf("-------------------实现拓扑排序---------------------------\n");
TopoSort(G);
}

对于这个问题呢,大家可以mark一下,然后,我会回头来说下的哦。


你可能感兴趣的:(C语言实现拓扑排序)