数据结构 拓扑排序问题

来源:百度知道 编辑:UC知道 时间:2024/06/12 02:27:04
拓扑排序:已知有九门课程,依次编号为C0至C8,在图一中给出了给出了各门课程之间先后关系。例如:C0是C2和C6的前序课程,而在选修C8之前,必须已经选修过C3和C7。要求存储该拓扑结构图,当用户从键盘输入任意课程的编号时,可打印出该课程的所有的前序课程。

c0-->c6-->c7-->c8
c0-->c2-->c3-->c8
c1-->c2
c1-->c4-->c5
c3-->c5
c4-->c3

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Node
{
intadjvex; //该弧所指向的顶点的位置

struct Node*next; //指向下一个条弧的指针
}ArcNode;
typedef struct Vertex
{
intindegree; //存放顶点的入度

ArcNode*first_arc; //指向第一条依附该顶点的弧的指针
}VNode;
int iarray; //用数组存储拓扑序列
typedef struct
{
int *base;
int *top;
}Stack;
Stack S; //用栈暂存所有入度为0的顶点
void InitStack(Stack *S)
{
S->base= (int *)malloc(10 * sizeof(int));
S->top =S->base;
}
void Push(Stack *S, int e)
{
*S->top++ = e;
}
void Pop(Stack *S, int *e)
{
*e =*--S->top;
}

void create_graph(VNode *vlist, int n) //用邻接表构造图
{
int i,position;