#include<stdio.h>
#include<stdlib.h>
#define NULL 0
#define MAX_VERTEX_NUM 8 /*顶点最大个数*/
typedef int VertexType; /*顶点元素类型*/
typedef struct ArcNode
{ int adjvex;
struct ArcNode *nextarc;
int weight; /*边的权*/
}ArcNode; /*表结点*/
typedef struct VNode
{
VertexType data;
ArcNode *firstarc;
}VNode /*头结点*/,AdjList[MAX_VERTEX_NUM];
AdjList creagreaph(AdjList g,int n) /*建立无向图*/
{ int e,i,s,d;
ArcNode *p,*q;
printf("结点数(n)和边数(e):");
scanf("%d,%d",&n,&e);
for(i=1;i<=n;i++)
{
printf("\t第%d个结点信息:",i);
scanf("%d",&g[i].data);
g[i].firstarc=NULL;
}
for(i=1;i<=e;i++)
{printf("\n第%d条边=>\n\t起点序号,终点序号:",i);
scanf("%d,%d",&s,&d);
p=(ArcNode*)malloc(sizeof(ArcNode));
q=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=d;
p->weight=g[d].data;
q->adjvex=s;
q->weight=g[s].data;
p->nextarc=g[s].firstarc;
g[s].firstarc=p;
p->nextarc=g[d].firstarc;
g[d].firstarc=q;
} return g;
}
void dfs(AdjList adj,int v) /*按深度遍历*/
{int i;int visit[3];
struct ArcNode*p;
p=(ArcNode*)malloc(sizeof(ArcNode));
for(i=1;i<=3;i++)
visit[i]=0;
visit[v]=1;
printf("%d",v);
p=adj[v].firstarc;
while(p!=NULL)
{
if(visit[p->adjvex]==0)
dfs(adj,p->adjvex);
p=p->nextarc;
}}
void main()
{ AdjList s;
int j;
s=creagreaph(s,j);
dfs(s,1);
}