关于#数据结构#的问题:输出有向图的拓扑排序序列(语言-c语言)

根据输入,输出有向图的拓扑排序序列。并画出有向图。输入:
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);
}
  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/7726049
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:【C语言】斐波那契数列,依次输出1 1 2 3 5 13等前10个数
  • 除此之外, 这篇博客: C程序设计中的 例6.1 对十个数组元素依次赋值为0,1,2,3,4,5,6,7,8,9,要求按逆序输出 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • #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;
    } 
    
    

    运行结果如下:
    在这里插入图片描述