根据输入,输出有向图的拓扑排序序列。并画出有向图。输入:
6,5,1
0,1
1,2
2,3
4,1
4,5
-1,-1
有向图:
运行结果:
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
#define Null 0
#define MAX 20
typedef struct ArcNode
{
int adjvex;
int weight;
struct ArcNode *nextarc;
}ArcNode,*AdjList;
typedef struct Graph
{
AdjList elem[MAX+1];
int vexnum;
int arcnum;
int GraphKind;
}Graph;
typedef struct Stack
{
int s[MAX];
int top;
}Stack;
void initStack(Stack *s)
{
(*s).top=0;
}
int Push(Stack *s, int e)
{
if((*s).top>=MAX) return 0;
else (*s).s[(*s).top++]=e;
}
int Pop(Stack *s, int *e)
{
if((*s).top<=0) return 0;
else *e=(*s).s[--(*s).top];
}
int StackEmpty(Stack s)
{
if(s.top==0)return 1;
else return 0;
}
void create(Graph *G)
{
int i, start, end; AdjList p;
for(i=0;i<=MAX;i++)
(*G).elem[i]=Null;
for(i=1;i<=(*G).arcnum;i++)
{
scanf("%d,%d",&start,&end);
p=(AdjList)malloc(sizeof(ArcNode));
p->adjvex=end;
p->nextarc=(*G).elem[start];
(*G).elem[start]=p;
if((*G).GraphKind==0)
{
p=(AdjList)malloc(sizeof(ArcNode));
p->adjvex=start;
p->nextarc=(*G).elem[end];
(*G).elem[end]=p;
}
}
}
int TopoSort(Graph G)
{
}
main()
{
Graph G;
printf("Please input the number of vex, arc and GraphKind:");
scanf("%d,%d,%d",&G.vexnum,&G.arcnum,&G.GraphKind);
create(&G);
printf("The outcome of TopoSort is:\n");
TopoSort(G);
}
#include <stdio.h>
int main()
{
int i,a[10];
for(i=0;i<=9;i++)
a[i]=i;
for(i=9;i>=0;i--)
printf("%d ",a[i]);
printf("\n");
return 0;
}
运行结果如下: