#include<stdlib.h>
#include<iostream.h>
#define MAX_VEXTEX_NUM 20
#define OK 1;
#define OVERFLOW 0
#define ERROR 0
#define NULL 0
#define FALSE -1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int status;
typedef int VertexType;
typedef int SElemType;
//邻接表表示图的结构
typedef struct ArcNode{
int adjvex;
struct ArcNode *nextarc;
}ArcNode;
typedef struct VNode{
VertexType data;
ArcNode *firstarc;
}VNode,AdjList[MAX_VEXTEX_NUM];
typedef struct{
AdjList vertices;
int arcnum,vexnum;
int indegree[MAX_VEXTEX_NUM];
}ALGraph;
//栈的顺序存储表示
typedef struct{
SElemType *base;//在构造之前和销毁之后,basea的值NULL
int top;//栈顶指针
int stacksize;//当前分配的存储空间
}SqStack;
status InitStack(SqStack &S)
{
S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S.base) exit(OVERFLOW);
S.top=-1;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
status Push(SqStack &S,SElemType e)
{
if(S.top>=STACK_INIT_SIZE)
{
S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base)
exit(OVERFLOW);
S.stacksize+=STACKINCREMENT;
S.top=STACK_INIT_SIZE;
}
S.base[++S.top]=e;
return OK;
status StackEmpty(SqStack S)
{
if(S.top==-1)
return OK;
else
return ERROR;
}
SElemType Pop(SqStack &S)
{
SElemType e;
if(S.top==-1)
return -1;
e=S.base[S.top--];
return e;
}
int Locate(ALGraph &G,VertexType V)
{
int i;
for(i=0;i<G.vexnum;i++)
if(G.vertices[i].data==V)
return i;
}
status CreatDG(ALGraph &G)
{
int i,k,j;
VNode *q;
ArcNode *p;
VertexType v1,v2;
cout<<"请输入总的结点数:";
cin>>G.vexnum;
cout<<"请输入总的边数:";
cin>>G.arcnum;
for(i=0;i<G.vexnum;i++)
{
cout<<"请输入第"<<i+1<<"个结点的值:";
cin>>G.vertices[i].data;
G.vertices[i].firstarc=NULL;
}
for(k=0;k<G.arcnum;k++)
{
cout<<"请你输入度第"<<k+1<<"条边的头结点和尾结点:";
cin>>v1;
cin>>v2;
i=Locate(G,v1);
j=Locate(G,v2);
q=&G.vertices[i];//q为第i个结点的指针
p=(ArcNode *)malloc(sizeof(ArcNode));
p->adjvex=j;
p->nextarc=q->firstarc;//尾插入结点
q->firstarc=p;
}
return OK;
}
void countin(ALGraph &G)
{
int i,k;
ArcNode *p;
for(i=0;i<G.vexnum;i++)
G.indegree[i]=0;
for(i=0;i<G.vexnum;i++)
for(p=G.vertices[i].firstarc;p;p=p->nextarc)
{
k=p->adjvex;
G.indegree[k]++;
}
}
status ToplogicalSort(ALGraph G)
{
int count;
int i,k;
ArcNode *p;
SElemType j;
SqStack S;
countin(G);//求各结点的入度
InitStack(S);
for(i=0;i<G.vexnum;i++)
if(!G.indegree[i]) Push(S,i);//入度为0的结点入栈
count=0;//对输入的顶点计数
while(!StackEmpty(S))
{
j=Pop(S);
cout<<"拓扑序列的第"<<++count<<"个数是"<<G.vertices[j].data<<endl;
for(p=G.vertices[j].firstarc;p;p=p->nextarc)
{
k=p->adjvex;
if(!--G.indegree[k])
Push(S,k);
}//for
}//while
if(count<G.vexnum)
return ERROR;//该有向图有回路
else
return OK;
}
void main()
{
ALGraph G;
int i;
CreatDG(G);
i=ToplogicalSort(G);
if(!i)
return;
}
编译时出现以下错误:
--------------------Configuration: 拓扑排序 - Win32 Debug--------------------
Compiling...
120.cpp
d:\c+++\msdev98\myprojects\拓扑排序\120.cpp(55) : error C2601: 'StackEmpty' : local function definitions are illegal
d:\c+++\msdev98\myprojects\拓扑排序\120.cpp(62) : error C2601: 'Pop' : local function definitions are illegal
d:\c+++\msdev98\myprojects\拓扑排序\120.cpp(70) : error C2601: 'Locate' : local function definitions are illegal
d:\c+++\msdev98\myprojects\拓扑排序\120.cpp(77) : error C2601: 'CreatDG' : local function definitions are illegal
d:\c+++\msdev98\myprojects\拓扑排序\120.cpp(108) : error C2601: 'countin' : local function definitions are illegal
d:\c+++\msdev98\myprojects\拓扑排序\120.cpp(121) : error C2601: 'ToplogicalSort' : local function definitions are illegal
d:\c+++\msdev98\myprojects\拓扑排序\120.cpp(149) : error C2601: 'main' : local function definitions are illegal
d:\c+++\msdev98\myprojects\拓扑排序\120.cpp(168) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
拓扑排序.exe - 8 error(s), 0 warning(s)
我的函数定义没有错误啊!
怎么老提示这样的错误信息呢?
麻烦大家帮我看一下!~!
[此贴子已经被作者于2006-11-21 12:10:26编辑过]