``````#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 10
struct ArcNode
ArcNode *next;
};
struct VertexNode
{
int vertex;
ArcNode *firstedge;
};
int visited[MAXSIZE];
typedef struct Graph
{
int vertexNum, arcNum;
}ALGraph ;
\\初始化邻接表
void InitALGraph(ALGraph g, int a[ ], int n, int e)
{
g.vertexNum=n; g.arcNum=e;
int i,j;
for (j=0; j<MAXSIZE; j++)
for (i=0; i<g.vertexNum; i++)
//输入顶点信息，初始化边表
{

}
int k;
for (k=0; k<g.arcNum; k++)
//输入边的信息存储在边表中
{
int j;
scanf("%d%d",&i,&j);

}
}

\\深度优先遍历
void DFSTraverse(ALGraph g , int v)
{
int i;
for(i=0;i<g.vertexNum;i++){
while(p){
DFSTraverse(g,i);
else p=p->next;
}
}
}

int main(){
ALGraph g;
int a[10]={1,2,3,4};
InitALGraph(g,a, 4, 4);
DFSTraverse(g , 0);
}

``````

``````#include<stdio.h>
#include<stdlib.h>
#define MAX_VER_NUM 50
int visited[MAX_VER_NUM]={0};
typedef char VertexType;
VertexType Q[MAX_VER_NUM];
typedef enum
{
DG,UDG
}GraphType;
typedef struct
{
VertexType vexs[MAX_VER_NUM];        //顶点向量
int arcs[MAX_VER_NUM][MAX_VER_NUM];  //邻接矩阵
int vexnum,arcnum;                   //图的当前顶点数和弧数
GraphType type;                      //图的种类标志
}MGraph;

//1. 根据名称得到指定顶点在顶点集合中的下标
//vex 顶点
//return 如果找到，则返回下标，否则，返回0
int getIndexOfVexs(char vex,MGraph *MG)
{
int i;
for(i=1;i<=MG->vexnum;i++)
{
if(MG->vexs[i]==vex)
{
return i;
}
}
return 0;
}

//2. 创建邻接矩阵
void create_MG(MGraph *MG)
{
int i,j,k;
int v1,v2,type;
char c1,c2;
printf("Please input graph type DG(1) or UDG(0):");
scanf("%d",&type);
if(type==1)
{
MG->type=DG;
}
else if(type==0)
{
MG->type=UDG;
}
else
{
printf("Please input correct graph type DG(1) or UDG(0)!");
return;
}
scanf("%d",&MG->vexnum);
scanf("%d",&MG->arcnum);
getchar();
for(i=1;i<=MG->vexnum;i++)
{
scanf("%c",&MG->vexs[i]);
getchar();
}
//初始化邻接矩阵
for(i=1;i<=MG->vexnum;i++)
{
for (j=1;j<=MG->vexnum;j++)
{
MG->arcs[i][j]=0;
}
}
//输入边的信息，建立邻接矩阵
for(k=1;k<=MG->arcnum;k++)
{
printf("Please input %dth arc v1(char) v2(char):",k);
scanf("%c %c",&c1,&c2);
v1=getIndexOfVexs(c1,MG);
v2=getIndexOfVexs(c2,MG);
if(MG->type==DG)
{
MG->arcs[v1][v2]=1;
}
else
{
MG->arcs[v1][v2]=MG->arcs[v2][v1]=1;
}
getchar();
}
}

//3. 打印邻接矩阵和顶点信息
void print_MG(MGraph MG)
{
int i,j;
if(MG.type==DG)
{
printf("Graph type: Direct graph\n");
}
else
{
printf("Graph type: Undirect graph\n");
}
printf("Graph vertex number: %d\n",MG.vexnum);
printf("Graph arc number: %d\n",MG.arcnum);
printf("Vertex set:");
for(i=1;i<=MG.vexnum;i++)
{
printf("%c",MG.vexs[i]);
}
for(i=1;i<=MG.vexnum;i++)
{
for(j=1;j<=MG.vexnum;j++)
{
printf("%d",MG.arcs[i][j]);
}
printf("\n");
}
}

//4.  从顶点V出发深度优先遍历图MG
void DFSMG(MGraph MG, VertexType V)
{
printf("%c",V);
int v,j;
v=getIndexOfVexs(V,&MG);
visited[v]=1;
for (j=1; j<=MG.vexnum; j++)
if (MG.arcs[v][j]==1 && visited[j]==0)
DFSMG(MG, MG.vexs[j]);

}

//5. 从顶点V出发广度优先遍历图MG
void BFSMG(MGraph MG, VertexType V)
{
int v,j;
int front=0, rear=0;   //假设采用顺序队列且不会发生溢出
printf("%c",V); v=getIndexOfVexs(V,&MG); visited[v]=1;  Q[++rear]=V;
while (front!=rear)
{
VertexType c=Q[++front];
v=getIndexOfVexs(c,&MG);
for (j=1; j<=MG.vexnum; j++)
if (MG.arcs[v][j]==1 && visited[j]==0 ) {
printf("%c",MG.vexs[j]); visited[j]=1; Q[++rear]=MG.vexs[j];
}
}

}

//主函数
int main(void)
{
MGraph MG;
create_MG(&MG);
print_MG(MG);
//printf("深度优先遍历序列为：\n");
//DFSMG(MG,'A');
//visited[MAX_VER_NUM]={0};
printf("\n广度优先遍历序列为：\n");
BFSMG(MG,'A');
return 0;
}

``````

``````#include<stdio.h>
struct MyStruct{
int a;
int *b;
MyStruct *c;
};

int main(){
MyStruct ms;
ms.a=5;
printf("%d\n",ms.a);
return 0;
}
``````

1个回答

``````The variable 'g' is being used without being inititalized

``````