main.cpp
#include<stdio.h>
#include<stdlib.h>
#include"Queue.h"
void main()
{
printf("拓扑排序是有向图\n");
LGraph Graph;
Graph = BuildGraph();
TopSort(Graph);
}
Queue.h
#ifndef QUEUE
#include"TopSort.h"
typedef struct {
int* base;
int front;
int rear;
}SqQueue;
void InitQueue(SqQueue* Q);
void getQueueLength(SqQueue Q);
void EnQueue(SqQueue* Q, int e);
void DeQueue(SqQueue* Q, Vertex* V);
bool IsEmpty(SqQueue Q);
#endif
#pragma once
Queue.cpp
#include"Queue.h"
#include"stdlib.h"
#include"stdio.h"
bool IsEmpty(SqQueue Q)
{
return Q.front == Q.rear ? true : false;
}
void InitQueue(SqQueue* Q)
{
Q->base = (int*)malloc(sizeof(int) * MaxVertexNum);
if (!Q)
{
printf("error");
}
else {
Q->front = Q->rear = 0;
}
}
void getQueueLength(SqQueue Q)
{
printf("\n length = %d \n", (Q.rear - Q.front + MaxVertexNum) % MaxVertexNum);
}
void EnQueue(SqQueue* Q, int e)
{
if ((Q->rear + 1) % MaxVertexNum == Q->front)
{
printf("full");
}
else
{
Q->base[Q->rear] = e;
Q->rear = (Q->rear + 1) % MaxVertexNum;
}
}
void DeQueue(SqQueue* Q, Vertex* V)
{
if (Q->rear == Q->front)
{
printf("no element");
}
else
{
*V = Q->base[Q->front];
Q->front = (Q->front + 1) % MaxVertexNum;
}
}
TopSort.h
#ifndef TOPSORT
#define MaxVertexNum 100
typedef int Vertex;
typedef int WeigthType;
typedef int DataType;
typedef struct ENode {
Vertex v1, v2;
WeigthType weigth;
}*PtrToENode;
typedef PtrToENode Edge;
typedef struct AdjVNode {
Vertex AdjV;
WeigthType weight;
struct AdjVNode* Next;
}*PtrToAdjVNode;
typedef struct VNode {
PtrToAdjVNode FirstEdge;
DataType Data;
}AdjList[MaxVertexNum];
typedef struct GNode {
int Nv;
int Ne;
AdjList G;
}*PtrToGNode;
typedef PtrToGNode LGraph;
void InsertEdge(LGraph Graph, Edge E);
LGraph BuildGraph();
void TopSort(LGraph Graph);
#endif
#pragma once
TopSort.cpp
#include"TopSort.h"
#include"Queue.h"
#include<stdio.h>
#include<stdlib.h>
Vertex TopOrder[MaxVertexNum];
LGraph CreatGraph(int VertexNum)
{
Vertex V;
LGraph Graph;
Graph = (LGraph)malloc(sizeof(struct GNode));
Graph->Nv = VertexNum;
Graph->Ne = 0;
for (V = 0; V < Graph->Nv; V++)
Graph->G[V].FirstEdge = NULL;
return Graph;
}
void InsertEdge(LGraph Graph, Edge E)
{
PtrToAdjVNode newNode;
newNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
newNode->AdjV = E->v2;
newNode->weight = E->weigth;
newNode->Next = Graph->G[E->v1].FirstEdge;
Graph->G[E->v1].FirstEdge = newNode;
}
LGraph BuildGraph()
{
int Nv, i;
Vertex V;
Edge E;
LGraph Graph;
printf_s("请输入插入节点的个数\n");
scanf_s("%d", &Nv);
Graph = CreatGraph(Nv);
printf_s("请输入插入边的个数\n");
scanf_s("%d", &(Graph->Ne));
if (Graph->Ne != 0)
{
E = (Edge)malloc(sizeof(struct ENode));
if (!E)
{
printf("E生成失败");
exit(-1);
return NULL;
}
for (i = 0; i < Graph->Ne; i++)
{
printf("请输入第%d条边: V1,V2,权重\n", i);
scanf_s("%d %d %d", &(E->v1), &(E->v2), &(E->weigth));
InsertEdge(Graph, E);
}
}
for (V = 0; V < Graph->Nv; V++)
{
printf("请输入第%d个顶点的数据\n", V);
scanf_s("%d", &(Graph->G[V].Data));
}
return Graph;
}
void TopSort(LGraph Graph)
{
int Indegree[MaxVertexNum];
int cnt = 0;
Vertex V;
PtrToAdjVNode W;
SqQueue Q;
InitQueue(&Q);
for (V = 0; V < Graph->Nv; V++)
{
Indegree[V] = 0;
}
for ( V = 0; V < Graph->Nv; V++)
{
for ( W = Graph->G[V].FirstEdge; W ; W = W->Next)
{
Indegree[W->AdjV]++;
}
}
for (V = 0; V < Graph->Nv; V++)
{
if (Indegree[V] == 0)
{
EnQueue(&Q, V);
}
}
while (!IsEmpty(Q))
{
DeQueue(&Q,&V);
TopOrder[cnt++] = V;
for ( W = Graph->G[V].FirstEdge; W ; W = W->Next)
{
if (--Indegree[W->AdjV] == 0)
{
EnQueue(&Q,W->AdjV);
}
}
printf("\n");
printf("%d\t",V);
}
}