源代码如下 c语言
先抛开输入输出格式,因联通与非联通会有不同的输出格式,采用非递归(栈)进行图的遍历是本题主体
#include<stdio.h>
#include<stdlib.h>
#define max 1005//增大
#define overflow -1
#define ERROR -1
typedef struct ArcNode{
int adjvex;
struct ArcNode * nextarc;
}ArcNode;
typedef struct VNode{
int cur;
ArcNode * firstarc;
}VNode,AdjList[max];
typedef struct{
AdjList vertices;
int vexnum,arcnum;
}ALGraph;
typedef struct{
int * base;
int * top;
}SqStack;
void InitStack(SqStack * S){
(*S).base = (int *)malloc(sizeof(int)*max);
if(!(*S).base) exit(overflow);
(*S).top = (*S).base;
}
void Push(SqStack * S,int e){
*((*S).top++) = e;
}
int Pop(SqStack * S){
int e;
if((*S).base == (*S).to