拓扑排序
#include"iostream.h"#define MaxNum 100;
typedef char VertexType;
typedef int EdgeType;
typedef struct {
VertexType vexs[MaxNum];
EdgeType edges[MaxNum][MaxNum];
int n,e;//定点数和边数
}MGraph;
typedef enum{False,True}Boolean;
void CreateM()
{
MGraph * G;
int i,j;
cout<<"Input the number of the number:";
cin>>G->n;
for(i=0;i<G->n;i++)
for(j=0;j<G->n;j++)
G->edges[i][j]=0;
cout<<"Input the nodes of edges:";
do{
cin>>i;
cout<<" ";
cin>>j;
G->edges[i][j]=1;
}while((i!=-1)&&(j!=-1))
}
void TopSort()
{
MGraph *G;
int i,j;
int degree[MaxNum];
Boolean visited[MaxNum];
Boolean flag=True;
cout<<endl<<"The Toplogical Order as follow:";
for(i=0;i<G->n;i++)
{
visited[i]=False;//初始化访问位
degree[i]=0;
}
while(flag==True)
{
for(i=0;i<G->n;i++)
for(j=0;j<G->n;j++)
degree[i]= G->edges[j][i]+degree[i];//计算每个顶点的入度
i=0;
while((i<G->n)&&(degree[i]!=0||visited[i]==True))i++;//入度为0的先出
if(i<G->n)
{
cout<<" "<<i;//每个顶点以空格隔开
visited[i]=True;
for(j=0;j<G->n;j++)
{
G->edges[i][j]=0;
degree[j]=0;
}
}
else
flag=False;
}
}
void main()
{
CreateM();
TopSort();
}
我用的是VC6.0。编译后总是报
--------------------Configuration: Topsort - Win32 Debug--------------------
Compiling...
top.cpp
E:\备份c盘\shuju\Topsort\top.cpp(8) : error C2143: syntax error : missing ']' before ';'
E:\备份c盘\shuju\Topsort\top.cpp(8) : error C2059: syntax error : ']'
E:\备份c盘\shuju\Topsort\top.cpp(8) : error C2238: unexpected token(s) preceding ';'
E:\备份c盘\shuju\Topsort\top.cpp(9) : error C2143: syntax error : missing ']' before ';'
E:\备份c盘\shuju\Topsort\top.cpp(9) : error C2059: syntax error : ']'
E:\备份c盘\shuju\Topsort\top.cpp(9) : error C2238: unexpected token(s) preceding ';'
E:\备份c盘\shuju\Topsort\top.cpp(9) : error C2059: syntax error : ']'
E:\备份c盘\shuju\Topsort\top.cpp(9) : error C2238: unexpected token(s) preceding ';'
E:\备份c盘\shuju\Topsort\top.cpp(23) : error C2109: subscript requires array or pointer type
E:\备份c盘\shuju\Topsort\top.cpp(23) : error C2106: '=' : left operand must be l-value
E:\备份c盘\shuju\Topsort\top.cpp(29) : error C2109: subscript requires array or pointer type
E:\备份c盘\shuju\Topsort\top.cpp(29) : error C2106: '=' : left operand must be l-value
E:\备份c盘\shuju\Topsort\top.cpp(31) : error C2143: syntax error : missing ')' before '}'
E:\备份c盘\shuju\Topsort\top.cpp(31) : error C2143: syntax error : missing ';' before ')'
E:\备份c盘\shuju\Topsort\top.cpp(31) : error C2143: syntax error : missing ';' before ')'
E:\备份c盘\shuju\Topsort\top.cpp(37) : error C2143: syntax error : missing ']' before ';'
E:\备份c盘\shuju\Topsort\top.cpp(37) : error C2143: syntax error : missing ';' before ']'
E:\备份c盘\shuju\Topsort\top.cpp(38) : error C2143: syntax error : missing ']' before ';'
E:\备份c盘\shuju\Topsort\top.cpp(38) : error C2143: syntax error : missing ';' before ']'
E:\备份c盘\shuju\Topsort\top.cpp(52) : error C2109: subscript requires array or pointer type
E:\备份c盘\shuju\Topsort\top.cpp(62) : error C2109: subscript requires array or pointer type
E:\备份c盘\shuju\Topsort\top.cpp(62) : error C2106: '=' : left operand must be l-value
Error executing cl.exe.
top.obj - 22 error(s), 0 warning(s)
我找不到问题出现在哪,请高手指点!!!急急