非递归深度优先遍历错在哪里啊
#include <stdio.h>#include <malloc.h>
#define MAXVEX 100
int INF=2147483647;
int visited[MAXVEX];
//邻接表存储结构类型定义
typedef char VertexType[10];
typedef struct edgenode
{
int adjvex;
int weight;
struct edgenode *nextarc;
}ArcNode;
typedef struct vexnode
{
VertexType data;
ArcNode *firstarc;
}VHeadNode;
typedef struct
{
int n,e;
VHeadNode adjlist[MAXVEX];
}AdjGraph;
//邻接图定义
typedef struct vertex
{
int adjvex;
VertexType data;
}VType;
typedef struct graph
{
int n,e;
VType vexs[MAXVEX];
int edges[MAXVEX][MAXVEX];
}MatGraph;
//建立图的邻接矩阵
void CreatGraph(MatGraph &g,int A[][MAXVEX],int n,int e)
{
int i,j;
g.n=n;g.e=e;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
g.edges[i][j]=A[i][j];
}
//建立表的邻接矩阵
void MatToAdj(MatGraph g,AdjGraph *&G)
{
int i,j;
ArcNode *p;
G=(AdjGraph*)malloc(sizeof(AdjGraph));
for(i=0;i<g.n;i++)
G->adjlist[i].firstarc=NULL;
for(i=0;i<g.n;i++)
for(j=g.n-1;j>=0;j--)
if(g.edges[i][j]!=0&&g.edges[i][j]!=INF)
{
p=(ArcNode *)malloc(sizeof(ArcNode));
p->adjvex=j;
p->weight=g.edges[i][j];
p->nextarc=G->adjlist[i].firstarc;
G->adjlist[i].firstarc=p;
}
G->n=g.n;G->e=g.e;
}
//输出图运算
void DispGraph(MatGraph g)
{
int i,j;
for(i=0;i<g.n;i++)
{
for(j=0;j<g.n;j++)
if(g.edges[i][j]<INF)
printf("%4d",g.edges[i][j]);
else
printf("%4s","∞");
printf("\n");
}
}
//输出表运算
void DispGraphtu(AdjGraph *G)
{
ArcNode *p;
int i;
for(i=0;i<G->n;i++)
{
printf("[%2d]",i);
p=G->adjlist[i].firstarc;
if(p!=NULL)
printf("→");
while(p!=NULL)
{
printf("%d(%d)",p->adjvex,p->weight);
p=p->nextarc;
}
printf("\n");
}
}
//以下是深度优先遍历的非递归算法
template<class T>
void MGraph ;DFSTraverse(int v)
{
top=1;
cout<<vertex[v];visited[v]=1;S[++top]=v;
while(top!=-1)
{
v=S[top];
for(j=0;j<vertexNum;j++)
if(arc[i][j]=1 && visited[j]=0)
{
cout<<vertex[j];
visited[j]=1;
S[++top]=j;
break;
}
if(j=vertexNum)top--;
}
}
//以下是深度优先遍历的递归算法
//void DFS(AdjGraph *G,int v)
//{
// int w;
// ArcNode *p;
// printf("%d",v);
// visited[v]=1;
// p=G->adjlist[v].firstarc;
// while(p!=NULL)
// {
// w=p->adjvex;
// if(visited[w]==0)
// DFS(G,w);
// p=p->nextarc;
// }
//}
//主函数调用
void main()
{
AdjGraph *G;
MatGraph g;
int n=5,e=6,i;
int A[MAXVEX][MAXVEX]={{2,2,2,0,0},{0,2,2,0},{0,2,0,0,2},{0,0,0,2,0},{0,0,0,0,2}};
CreatGraph(g,A,n,e);
printf("图G中的存储结构:\n");DispGraph(g);
MatToAdj(g,G);
printf("图G的邻接表:\n");DispGraphtu(G);
for(i=0;i<G->n;i++) visited[i]=0;
printf("深度优先遍历:\n");
printf("DFS:");DFS(G,0);printf("\n");
}